how to do jquery function if wordpress metabox checkbox is checked Sync in add new post area -
in wordpress backend -> add new post area, how after metabox -> checkbox checked, example when checked category:
this the checkbox before checked:
<input value="1" type="checkbox" name="post_category[]" id="in-category-1">
this the checkbox after checked:
<input value="1" type="checkbox" name="post_category[]" id="in-category-1"> ::before </input>
so, how detect checkbox checked , function after checked?
you can achieve using jquery.
!(function($){ $(document).ready(function(){ $('#categorychecklist').on('change', 'input[type=checkbox]', function(){ if ($(this).is(':checked')) alert('category' + $(this).closest('label').text() + ' added.'); else alert('category' + $(this).closest('label').text() + ' removed.'); }); }); })(window.jquery);
/wp-content/themes/themename/js/post-jquery.js
i have category added or removed after being ticked or unticked (change event).
to attach jquery. check if current page adding/editing post get_current_screen()
so.
<?php function add_script_to_post() { $current_screen = get_current_screen(); if ( $current_screen->base == 'post' ) { // check if adding/editing post wp_enqueue_script('post-jquery', get_stylesheet_directory_uri() . '/js/post-jquery.js'); } } add_action('current_screen', 'add_script_to_post'); ?>
/wp-content/themes/themename/functions.php
Comments
Post a Comment