calling jquery function on plugin page

I am trying to call the following script on a plugin admin page:

<?php
function add_match_attendance_scripts() {

$output="<script type="text/javascript">

            jQuery(function () {
$("#seatt_start").datepicker({dateFormat: "dd-mm-yy"});
$("#seatt_expire").datepicker({
    dateFormat: "dd-mm-yy",
    firstDay: 1,
    onSelect: function (prev_date) {
        prev = prev_date.split("-");
        var date = new Date(prev[2], prev[1] - 1, prev[0]);
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        var new_date = new Date(y, m, d - 7);
        $("#seatt_start").removeAttr("disabled").datepicker("setDate", new_date);
     }
   });
});

</script>";

                echo $output;
        } 
            add_action('admin_head','add_match_attendance_scripts');
?>

I already have enqueued jquery-ui-datepicker for all admin pages and it works correctly on all admin pages using a similar function:

add_action('admin_head','add_report_custom_scripts');

function add_report_custom_scripts() {
global $report_custom_meta_fields, $post;

$output="<script type="text/javascript">
            jQuery(function() {";

foreach ($report_custom_meta_fields as $field) { // loop through the fields looking for certain types
    // date
    if($field['type'] == 'date')
        $output .= 'jQuery(".datepicker").datepicker({ dateFormat: "dd-mm-yy", firstDay: 1 });';

}

$output .= '});
    </script>';

echo $output;
}

but apparently this script runs on my plugin page and not the one I wanted. So how can I specify which script goes where seeing as they are both admin pages?

1 Answer
1

You can review the explanation here to include any script here

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugins_url('/myscript.js', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

The above code will add the script in all your admin pages.

In order to identify the page you may follow this:

if (isset($_GET['page']) && $_GET['page'] == 'wp-accordion') {
    // do all of your add_action() hooks here
}

Here “page” is the get variable which you defined in your created plugin while adding menus.

I hope you find my answer useful.

Leave a Comment