I am building my first WP plugin, it should load some JS and CSS files only on specific pages selected through a form available in the plugin Admin area. Once selected in the form, Page Title gets stored in the DB wp_options table, then the data is pulled back out in a variable named $page_selected.
To load JS and CSS files only in the pages selected in form, I wanted to use the is_page() function, passing the $page_selected variable as a parameter.

 function my_custom_tooltip() {
    if (  is_page($page_selected) ) {
        wp_enqueue_style( 'custom_tooltip_frontend_css', plugins_url('custom-image-tooltip/custom-image-tooltip.css') );
        wp_enqueue_script( 'custom_tooltip_frontend_js', plugins_url('custom-image-tooltip/custom-image-tooltip.js'), array('jquery'), '', true );
    }
} add_action('wp_enqueue_scripts', 'my_custom_tooltip');

Unluckily In my case that conditional statement doesn’t work correctly. Is there any way to achieve the same result, matching the page selected in the form by the user with the current Page being displayed?

4 s
4

If $page_selected returns a string containing the page title, it should work fine. I have tested it and is_page() accepts page title, page ID and the slug.

If you are facing issues, it will be better to store page ID in the database and use it with is_page()

Please refer to https://codex.wordpress.org/Function_Reference/is_page for more details on the possible arguments that can be passed to this conditional statement.

Leave a Reply

Your email address will not be published. Required fields are marked *