10

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?

5
  • 1
    is_page() accepts variables as parameter. In fact, you can use variables as parameter for any PHP function that accept parameters. Why do you say it doesn't? It should work if you pass the correct value in the variable. I think the problem is that the variable has not the value you think it has.
    – cybmeta
    Jan 5, 2016 at 17:20
  • I tried to store in a php variable the Page Title or the Page Slug (e.g. $my_page_id = 'about';) and then use that variable as an is_page() parameter, but it didn't work. In the WP codex I found out that is_page() accepts Page ID, Page Title or Page Slug as parameters. I am new to PHP and WP and I thought that meant you could only pass strings or integers as an is_page() function parameter. I corrected my post to avoid providing misleading information to other users.
    – RaymondMik
    Jan 6, 2016 at 9:40
  • can you show a var_dump($page_selected)
    – WPTC-Troop
    Jan 6, 2016 at 9:56
  • 1
    Now I think that your question is not about your real problem. I think you need to know how to get the value set in the plugin's form. I just noted that $page_selected variable is not defined in your code. Can you show us how have you built the form in the admin area and how do you store the values set in that form?
    – cybmeta
    Jan 6, 2016 at 10:12
  • This is how I built my form in the Admin area: <select name="page_dropdown"> <option value=""><?php echo esc_attr( __( 'Select page' ) ); ?</option> <?php $pages = get_pages(); foreach ( $pages as $page ) { $option = '<option>'; $option.= $page->post_title; $option .= '</option>'; echo $option; } ?> </select> Let's say I select "test_page_1" in my form - var_dump($page_selected) = string(11) "test_page_1";
    – RaymondMik
    Jan 6, 2016 at 10:32

4 Answers 4

9

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.

0
7

I make some pretty change and I hope this help you well. Try below code:

function my_custom_tooltip() {
  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 );
} 

if(is_page($page_selected)){
    add_action('wp_enqueue_scripts', 'my_custom_tooltip');
}

Here, make sure about $page_selected.

NOTE: You should always enqueue your scripts in a theme's functions.php file - not a template file. Template files are just Not The Right Place 99.9% of the time.

You can use is_page() in multiple form like:

// When any single Page is being displayed.
is_page();

// When Page 42 (ID) is being displayed.
is_page( 42 );

// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );

// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );

// Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "Contact".  
is_page( array( 42, 'about-me', 'Contact' ) );

Note: The array ability was added at Version 2.5.

1
  • To work properly, scripts need to be enqueued at the right time during the page load. If they're loaded too late they may not work at all. So you need to hook them to the wp_enqueue_scripts action. Jan 9, 2016 at 13:00
2

If you're running this code inside a template file, it will work just fine, but if this code is in functions.php or in a plugin file, it will throw this error in debug.log:

PHP Notice: is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false.

In this case, you could use the template_redirect action hook, as explained here.

0

Untested...but something like this should work to get the name (i.e. slug) of the page currently displayed:

function my_custom_tooltip() {
    global $post;
    if ( is_object( $post ) && $post->post_type=='page' ) {
        if ( $post->post_name == $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 );
        }
    }
}

The compare to $page_selected is of course expecting it to be in "slug" format as well...or if comparing to page title you could use $post->post_title instead of $post->post_name.

Note, this is an example where using PHP classes is helpful, to avoid having to use a global variable ($page_selected) inside that function...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.