WP Plugin: Print javascript in header

[I am new to wordpress programming.]

I am developing a plugin. The plugin needs javascript code in header. In order to print the code only in header of plugin's setting page. I am unable to do that. I have referred instructions here

The plugin works fine if javascript is printed using this plugin

I also tried to add code within body (after div) by below method....

include( plugin_dir_path( __FILE__ ) . 'ipn/javascript.php'); 

But that to did not work.

Check the plugin code....

add_action( 'admin_enqueue_scripts', 'my_enqueued_assets' );  function my_enqueued_assets() { wp_enqueue_script('Google_jquery', 'http://code.jquery.com/jquery-2.2.4.min.js');  }  add_action('admin_menu', 'my_plugin_menu'); function my_plugin_menu() { add_menu_page('My Plugin Settings', 'Plugin Settings', 'administrator', 'my-plugin-settings', 'my_plugin_settings_page', 'dashicons-admin-generic'); }  function my_plugin_settings_page() { global $my_plugin_settings;  echo 'HTML Form code here'; add_action( "admin_head-{$my_plugin_settings}", 'my_admin_head_script' ); } function my_admin_head_script() { ?>     // javascript code that i want to print in header  <script type="text/javascript">$(function() { var scntDiv = $('#p_scents'); var i = $('#p_scents p').size() + 1;  $(document).on('click','#addScnt', function() {         $('html code here').appendTo(scntDiv);         i++;         return false; });  $(document).on('click','#remScnt', function() {          if( i > 2 ) {                 $(this).parents('p').remove();                 i--;         }         return false; }); });</script> // javascript code end    <?php } add_action('admin_head', 'my_custom_fonts');  function my_custom_fonts() { echo 'CSS style here'; } 


To only insert code into the header section in the admin area on single plugin pages you need to alter your add_action()function.

add_action('admin_head-pageof_thisplugin/thisplugin', 'thisplugin_adminhead');

function thisplugin_adminhead() {
    // here goes your content
    echo '<script>'
         .'/* ... */'

where pageof_thisplugin/thispluginis the hook slug that returns out of the function that creates your page like add_submenu_page(), add_options_pageoradd_management_page`.

Source and further details in the WordPress Codex

Category: plugins Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.152 (s). 12 q(s)