I'm creating a custom plugin for WordPress and I'm trying to execute javascript file only on plugin deactivation.
I'm using register_deactivation_hook() and wp_enqueue_script() to execute file only on plugin deactivation.
There are no errors in the code since the javascript code works well if it is called outside the register_deactivation_hook()
This is what I've tried so far:
register_deactivation_hook( __FILE__, 'deactivation' );
function deactivation() {
function delete_rest_api() {
wp_enqueue_script('deactivation_data_api', plugins_url('assets/js/deactivation_data_api.js', __FILE__));
}
add_action( 'admin_enqueue_scripts', 'delete_rest_api' );
}
In the end, the plugin needs to execute that javascript file only during plugin deactivation.
Thanks in advance.
Related
I have found out JQuery is loading twice on my Wordpress website and is causing some plugins to break. I have done some research and think I need to remove some JQuery code from the header.php file, and then insert some new JQuery code into the functions.php file. Trouble is I am not exactly sure what code to remove/add. Can anyone help me stop JQuery running twice?
To remove the default WordPress jQuery file you can use the following:
function remove_jquery(){
wp_deregister_script('jquery');
wp_dequeue_script('jquery');
}
add_action('wp_enqueue_scripts','remove_jquery');
That should deregister and dequeue the native jQuery file and keep the one that you have in your header.php file.
function dequeue_script() {
wp_dequeue_script( 'jquery' );
}
add_action( 'wp_print_scripts', 'dequeue_script', 100);
I am developing a plugin in WordPress and for the same plugin in settings page I want to integrate a jquery in backend on edit.php. I have followed the below method but it is not working
function cs_colorpicker_js() {
wp_enqueue_script('cs_colorpicker_js',
plugin_dir_url(__FILE__).'js/cs_colorpicker.js'
);
}
add_action( 'admin_enqueue_scripts', 'cs_colorpicker_js' );
here is the screenshot of one error that i have found. http://goo.gl/wmgH60
Use the third variable of Wp_enqueue_script() like,
function cs_colorpicker_js() {
wp_enqueue_script('cs_colorpicker_js',
plugin_dir_url(__FILE__).'js/cs_colorpicker.js',
array('jquery') // jquery dependency
);
}
I need to know how to take the output of a custom built Wordpress plugin and output it onto a specific page.
I know that I need to use the add_action() / add_filter() functions to call the function which outputs the plugins output when a wordpress hook function runs.
Currently I am using the 'the_content' hook.
This outputs my plugins output to all pages in my theme which call the the_content() function.
Which hook can I use to make the output only appear on a specific page.
Also
It would be useful to know how to create a page using my plugin.
Checkout is_page()
if (is_page(123)) {
// put your hooks here
}
put your code in a file inside plugin directory
then
use this
function page_directory()
{
if(is_page('123')){
$dir = plugin_dir_path( __FILE__ );
include($dir."custom/page.php");
die();
}
}
add_action( 'wp', 'page_directory' );
I am creating a wordpress plugin from which user can select different styles from the plugin options panel. I want to enqueue css file via wp_enqueue_style() according to the user input.
I am getting error when I want to get the metabox value inside the plugins php file.
Error Says:
Calling undefined variable $... on line no ..."
Is there any way I can get the value of the metabox inside php file and enqueue the appropiate css file according to the user input on dashboard.
I am using Custom-Metaboxes-and-Fields-for-WordPress plugin to retrieve user input from plugin option panel for this purpose
My code:
function easyloader_theme() {
global $post;
$theme = easyloader_get_option( 'easyloader_theme' );
wp_register_style('easyloader_style', plugins_url('themes/pace-theme-'. $theme .'.css',__FILE__),'','1.0.0.', false);
wp_enqueue_style('easyloader_style');
}
add_action( 'init', 'easyloader_theme');
I can easily get value of this user input in any when I paste this code in themes php file like (header.php, page.php, footer.php ) via
<?php global $post; $theme = easyloader_get_option( 'easyloader_theme' ); echo $theme; ?>
I just want to get the value inside my plugin php file not in the theme file.
The proper way to enqueue is with the following action:
add_action( 'wp_enqueue_scripts', 'easyloader_theme' );
function easyloader_theme() {
/* Enqueue Scripts AND Styles */
}
Looks like the plugin has some internal cache, but at the end you can try using WP function directly:
get_option( 'easyloader_theme' );
I want to register a script using when my plugin is activated. So I placed:
register_activation_hook(__FILE__, 'register_script');
function register_script(){
wp_register_script('addjs','http://example.com/exmple.js');
}
Next I created a meta box and enqueue the script in the add_meta_boxes hook:
wp_enqueue_script('addjs');
Why that doesn't work? My JS file is just a simple alert function. However, why when I register my script using init hook, or other hook, my code works?
Registering the script doesn't actually enqueues the script. And that's what needs to be optimized.
register_activation_hook runs only once, registering your script there won't keep it registered in subsequent WordPress loads. Neither the action hook init is the place to do this. It's either wp_enqueue_scripts (frontend) or admin_print_scripts (backend), despite their names they're used to enqueue styles and scripts.
Loading your scripts only in specific places is a great practice. Once my admin area broke because a theme was loading its scripts/styles everywhere, not only on the frontend. Crappy code at its best.
The main difference is that if you register a script, you'll only need to call wp_enqueue_script($handle) in other places of your code. But if it's not the case, you can drop the register part and enqueue directly.
A small trick is to add your wp_enqueue_script and wp_enqueue_style inside your add_meta_box() callback, and it will only load together with the Meta Box. Works the same for Shortcodes.
Another option,
add_action( 'admin_menu', function()
{
$page = add_submenu_page( $args );
add_action( "admin_print_scripts-$page", 'your_callback' );
});
Or,
add_action( 'wp_enqueue_scripts', function() {
if( is_single() )
wp_enqueue_style( $args );
});
References:
- conditional-wp-enqueue-script-on-a-page
- when-to-use-add-actioninit-vs-add-actionwp-enqueue-scripts
- register-and-enqueue-conditional-browser-specific-javascript-files
- how-to-load-scripts-and-css-for-admins-only-when-editing-or-adding-posts