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?
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.var_dump($page_selected)
$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?<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";