How to add custom javascript to WordPress Admin?

Cover Image for How to add custom javascript to WordPress Admin?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

šŸ“ Title: Adding Custom JavaScript to WordPress Admin: A Simple Plugin Solution āœØšŸ”Œ

šŸ‘‹ Hey there, WordPress admin wizards! We've got a cool trick up our sleeves today to help you add some custom JavaScript to the Edit Post page in a simple and plugin-friendly way. No more hacking admin template files! šŸ’ŖšŸ’»

šŸ” Problem Statement: So, you're itching to add that cool jQuery code to show a div when someone presses "Publish" on the Edit Post page. But here's the catch - you want to achieve this using a plugin, not by tinkering with admin template files. Don't worry, we got you covered!

šŸ’” The Solution: A Plugin Magic āœØāœØ You can achieve this customization easily by creating a custom plugin that injects your desired JavaScript code. Let's break it down into simple steps:

  1. šŸ“‚ Create a New Plugin: Start by creating a new folder in your WordPress wp-content/plugins/ directory. Give it a catchy name like custom-javascript-admin-plugin. Inside this folder, create a new file called custom-javascript-admin-plugin.php.

  2. āœļø Add Plugin Headers: Open custom-javascript-admin-plugin.php in your favorite code editor and add the following plugin headers:

<?php
/**
 * Plugin Name: Custom JavaScript Admin Plugin
 * Description: Add custom JavaScript code to WordPress admin.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 */
  1. šŸ›  Enqueue Your JavaScript: We'll now add a function to enqueue your custom JavaScript code. Add the following code to custom-javascript-admin-plugin.php:

<?php
function custom_javascript_admin_enqueue_scripts() {
    $screen = get_current_screen();

    // Target the Edit Post screen
    if ($screen->id === 'post') {
        wp_enqueue_script(
            'custom-javascript-admin-script',
            plugin_dir_url(__FILE__) . 'js/custom-javascript-admin.js',
            ['jquery'],
            '1.0.0',
            true
        );
    }
}
add_action('admin_enqueue_scripts', 'custom_javascript_admin_enqueue_scripts');
  1. āš”ļø Create Your JavaScript File: Inside your plugin folder, create a new folder called js. In this js folder, create another file called custom-javascript-admin.js. This is where you'll write your jQuery code to show the desired div when the "Publish" button is pressed.

jQuery(document).ready(function ($) {
    $('#publish').click(function () {
        $('#your-div').show();
    });
});
  1. šŸš€ Activate Your Custom Plugin: Once you've completed all the steps, simply log in to your WordPress admin area, navigate to the "Plugins" page, and activate your "Custom JavaScript Admin Plugin".

āœØ Voila! That's it! Now, whenever someone presses the "Publish" button on the Edit Post page, your custom div will magically appear. No more hacking, just pure plugin magic! šŸŽ©āœØ

šŸ“£ Call to Action: Try this simple plugin solution today and customize your WordPress admin like a pro! Don't forget to share your experience and other cool customizations in the comments below. Happy coding! šŸ’»šŸŽ‰

šŸ‘€ Example Use Case: Let's say you have a website where you frequently collaborate with other authors. You can create a custom div that shows a message like "Collaboration Request Sent!" when you hit the "Publish" button. This simple JavaScript addition can help streamline your collaboration process and keep everyone in the loop. Isn't that neat?


Note: If you're looking to add JavaScript code to the frontend of your WordPress website, check out our blog post on "How to Add Custom JavaScript to WordPress Theme" for the complete guide.

šŸ’” TL;DR (Too Long; Didn't Read): Avoid hacking admin template files and create a custom plugin to add JavaScript code to the WordPress Admin. Follow our easy steps: create a new plugin, enqueue your JavaScript, create a JavaScript file, and activate the plugin. Voila! Your custom JavaScript will work like a charm. Happy coding! šŸ’ŖšŸ’»


šŸ”„ Enjoyed this blog post? We have more amazing WordPress tips and tricks waiting for you! Subscribe to our newsletter for regular updates and be the first to know about all the cool things you can do with WordPress. šŸ’ÆšŸ’Œ

šŸ“ Note: Feel free to adapt this blog post according to your writing style and the target audience of your tech blog. Add relevant images, GIFs, or even emojis to make it more engaging and share-worthy. Happy writing! šŸš€āœļø


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello