I want to add some custom jquery code to the Edit Post page, something really simple like showing a div when someone presses Publish.
The only restriction is that I want to achieve this through the use of a plugin, not hacking the admin template files.
I've tried echoing some script tags using some actions but it doesn't seem to be the way.
Use the admin_enqueue_scripts action and the wp_enqueue_script method to add custom scripts to the admin interface.
This assumes that you have myscript.js in your plugin folder. Change accordingly. The my_custom_script handle should be unique for your module and script.
function my_enqueue($hook) {
// Only add to the edit.php admin page.
// See WP docs.
if ('edit.php' !== $hook) {
return;
}
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts', 'my_enqueue');
There is a snippet for Your functions.php file :
function custom_admin_js() {
$url = get_bloginfo('template_directory') . '/js/wp-admin.js';
echo '"<script type="text/javascript" src="'. $url . '"></script>"';
}
add_action('admin_footer', 'custom_admin_js');
Works fine on Wordpress 3.2.1.
<?php
function add_jquery_data() {
global $parent_file;
if ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' && isset( $_GET['post'] ) && $parent_file == 'edit.php') {
// Do some stuff.
}
}
add_filter('admin_head', 'add_jquery_data');
?>
admin_enqueue_scripts and wp_enqueue_script are the preferred way to add javascript files to the dashboard.
// I'm using an anonymous function for brevity.
add_action( 'admin_enqueue_scripts', function() {
wp_enqueue_script( 'handle', plugin_dir_url( __FILE__ ) . '/script.js' );
} );
If you want to output the javascript using your PHP function however, wp_add_inline_script doesn't seem to work. Instead, you can use admin_print_scripts to directly echo out the script, including the script tags themselves. Just ensure to set the priority high so that it loads after any required libraries, such as jQuery.
add_action( 'admin_print_scripts', function() {
// I'm using NOWDOC notation to allow line breaks and unescaped quotation marks.
echo <<<'EOT'
<script type="text/javascript">
jQuery(function($){
// Do stuff here.
});
</script>
EOT;
}, PHP_INT_MAX );
If you want to get fancy and filter where you want to load the file or not, best to use get_current_screen.
function myproject_admin_enqueue() {
$screen = get_current_screen();
// load on the NEW and EDIT screens of all post types
if ( 'post' === $screen->base ) {
wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'all-admin.js');
}
// load on the NEW and EDIT screens of one post type
if ( 'post' === $screen->base && 'myposttype' === $screen->post_type ) {
wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'mypostype-admin.js');
}
// load only when adding a NEW post, not when editing an existing one.
if ( 'post' === $screen->base && 'add' === $screen->action ) {
wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'new-admin.js');
}
}
add_action('admin_enqueue_scripts', 'myproject_admin_enqueue');
Directly adding wp_enqueue_script to your code doesn't include the script in new versions of Wordpress (5.0 above).
The better way is to register the script with wp_register_script first to create a handle and then enqueue that handle.
function custom_script_in_admin($hook) {
if ('edit.php' !== $hook) {
return;
}
wp_register_script( 'custom_handle_name',plugin_dir_url( __FILE__ ) . '/script.js', '',true );
wp_enqueue_script('custom_handle_name');
}
add_action('admin_enqueue_scripts', 'custom_script_in_admin');
Somehow the accepted answer didn't work for me. Here is another solution I didn't found in the answers and this is what did it for me, WP 5.x, adding some css and js for my backend...
function suedsicht_theme_add_editor_assets() {
wp_enqueue_style( 'custom-gutenberg-stylesheet', get_template_directory_uri() . '/css/custom-editor-style.css', array(), wp_get_theme()->get( 'Version' ), 'all' );
wp_enqueue_script('suedsicht-admin-js', get_template_directory_uri() . '/js/admin.js');
}
add_action( 'enqueue_block_editor_assets', 'suedsicht_theme_add_editor_assets' );
By using the admin enqueue script hook you can easily add the script file for the admin panel.
function custom_admin_js() {
wp_enqueue_script( 'custom_wp_admin_js', get_template_directory_uri() . '/new-assets/js/admin_section.js', false, '1.0.0' );
wp_enqueue_script( 'custom_wp_admin_js' );
}
add_action('admin_enqueue_scripts', 'custom_admin_js');
Related
I want to increase my page speed by deregistering unnecessary external resources. I already managed to remove most of the external scripts, Elementor loads by default on the frontend. However, I can't remove the jQuery plugin Sticky somehow. I guess it has to do with being a part of Elementor Pro.
I've already tried to look under jQuery depencies, however that didn't work for me.
function remove_jquery_sticky() {
if ( ! is_admin()) {
wp_deregister_script( 'sticky' );
}
}
add_action( 'elementor/frontend/after_register_scripts', 'remove_jquery_sticky' );
I expect the jQuery plugin not to load on the frontend, however it still does.
I have found a solution that works for me. If you've a cleaner solution, please let me know :)
if(is_front_page()) {
// Dequeue and deregister elementor-sticky
wp_dequeue_script( 'elementor-sticky' );
wp_deregister_script( 'elementor-sticky' );
// Dequeue and deregister elementor-pro-frontend
wp_dequeue_script( 'elementor-pro-frontend' );
wp_deregister_script( 'elementor-pro-frontend' );
// Re-register elementor-frontend without the elementor-sticky dependency.
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script(
'elementor-pro-frontend',
ELEMENTOR_PRO_URL . 'assets/js/frontend' . $suffix . '.js',
[
'elementor-frontend-modules',
],
ELEMENTOR_VERSION,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'elementor_pro_frontend_scripts' );```
Elementor and Elementor PRO register and enqueue some scripts with dependences. For remove you need deregister and register again without specific script (example without 'elementor-sticky').
if(is_front_page()) {
// Dequeue and deregister elementor-pro-frontend
wp_deregister_script( 'elementor-pro-frontend' );
// Re-register elementor-frontend without the elementor-sticky dependency.
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script(
'elementor-pro-frontend',
ELEMENTOR_PRO_URL . 'assets/js/frontend' . $suffix . '.js',
[
'elementor-frontend-modules',
],
ELEMENTOR_VERSION,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'elementor_pro_frontend_scripts', 20 );
If you know the name of the action that is being added you can use the function remove_action( $tag, $function_to_remove, $priority ) or you can use wp_dequeue_script( $handle )
https://codex.wordpress.org/Function_Reference/remove_action
https://codex.wordpress.org/Function_Reference/wp_dequeue_script
I have a page speed issue, the theme I bought is really crappy but i cannot change it now. I use WP_rocket, server have HTTP2 but still it is to many resources to load. So i try to reduce numbers of styles by wp_deregister_style and load it only when need. For example contact-form-7 front-end style I need only in .../contact page. It good idea ? Or it could be harmful?
function rs_deregister_css () {
global $wp;
$url = home_url( $wp->request);
$contakt = strpos($url,'contakt');
if (!$contakt) {
wp_deregister_style('contact-form-7');
}
}
add_action( 'wp_print_styles', 'rs_deregister_css', 99);
Yes, its a very good idea since you only use the contact form in the contact page, don't forget to deregister the javascript file, too
if (!$contakt) {
wp_deregister_style('contact-form-7');
wp_deregister_script('contact-form-7');
}
Yes, it would be a good idea for your load time. But I suggest you to load style on specific instead of checking the URL and unload it every time.
Try to read Conditional Tags
function my_enqueue_stuff() {
if ( is_front_page() ) {
/** Call landing-page-template-one enqueue */
wp_enqueue_style( 'your-style-handle', get_stylesheet_directory_uri() . '/yourfile.css' );
} else {
/** Call regular enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
EDIT
Since you only want to the style or javascript load for specific pages, then add this code to your functions.php:
function remove_wpcf7_extras() {
remove_action('wp_print_scripts', 'wpcf7_enqueue_scripts');
remove_action('wp_print_styles', 'wpcf7_enqueue_styles');
}
if( !is_page('contact') ) {
add_action('wp_head', 'remove_wpcf7_extras');
}
I am trying to prevent all of the different scripts from my shop ( jigoshop ) from loading on every page load. I have been using the wp_deregister_script function but it wont work. My question is, if there is a way that the wp_deregister_script function could be blocked by the plugin itself or any other function?
This is the script i am currently using:
function js_css_control()
{
$link = $_SERVER['REQUEST_URI'];
$teile = explode("/", $link);
if($link!="/")
{
$seite = $teile[1];
}
else
{
$seite = "start";
}
if($seite=="start")
{
wp_deregister_script( 'jigoshop_global' );
wp_deregister_script( 'prettyPhoto' );
wp_deregister_style ( 'jigoshop_styles' );
wp_deregister_style ( 'jigoshop-jquery-ui' );
wp_deregister_style ( 'jigoshop-select2' );
}
}
add_action('wp_enqueue_scripts', 'js_css_control');
The function that may suit your purpose better is wp_dequeue_script. You can do everything just about the same except change deregister to dequeue.
Also, if that doesn't work and for future reference, try changing the hook that you use in add_action, for instance template_redirect may be a choice in case wp_enqueue_scripts doesn't pan out.
I understand that the Wordpress global $post may not work until a certain point because it hasnt been loaded, but Im wondering is there a workaround for this.
Basically Im building a plugin specifically for a client. At the moment the variable is showing nothing.
This code is in a plugin in the plugins folder.
Im looking to make sure it only loads javascript (Ive left out that bit) when on specific pages (selected by the user), at the moment its loading on all pages.
My other option is to do it all on a php template, but to be honest I wanted to write it as a plugin with a view to customizing it for more generic use in the future, plus I have little experience with plugins so Im trying to improve that side of things also.
function include_js() {
global $post;
print_r($post);
if(is_object($post) && $post->ID == 14 ){
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'init', 'include_js' );
EDIT: I realised my main issue is that I want to include the javascript and because of that I need wp_enqueue_script , but I can only seem to get that work if you use the init action, which happens before the loop.
After seeing your edit, try hooking to wp_enqueue_scripts instead of init. Like this:
function include_js() {
global $post;
print_r( $post );
if ( is_object( $post ) && $post->ID == 14 ) {
// do stuff
wp_enqueue_script( 'include-map', plugin_dir_url( __FILE__ ) . 'map.js' );
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
Ref: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You should test $wp_query->queried_object_id instead of $post->id:
function include_js() {
global $wp_query;
if ( $wp_query->queried_object_id == 14 ) {
// do stuff
//...
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
There is no need for any globals etc. You can simply use is_single($post) to check if you are on a specific single post and then enqueue your script. You should always always use the wp_enqueue_scripts hook to hook your function to to enqueue scripts and styles
function include_js() {
if(is_single(14)) {
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'wp_enqueue_scripts', 'include_js', 999 );
Remeber also, always add priority when adding custom scripts/styles. See add_action( $hook, $function_to_add, $priority, $accepted_args )
I am trying to add a custom script via my functions.php file.
Here is the code. First I am loading jQuery and then the script
When I view my source code I can not see a link to the script.
By the way, let me know if you need to see more of my code I wasn't sure how much I should post.
// jQuery
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
// show Hide
function add_my_script() {
wp_enqueue_script(
'show-hide',
get_template_directory_uri() . '/js/show-hide.js',
array('jquery')
);
}
You're missing:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
to hook add_my_script() function on to action.