Created
October 13, 2017 05:15
-
-
Save digvijayad/7657037be67c90220f7b3bcec8baccc1 to your computer and use it in GitHub Desktop.
Dynamically Hide/show Meta box on run time during category selection for themes and Plugins.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hide/Show a any div element by selecting a categories for a wordpress theme. | |
* | |
* Copy all of these functions to your function.php file, | |
* | |
* and call => do_action( | |
* 'mythemeprefix_hide_meta_for_categories', | |
* array("category1","category2"), | |
* 'my_meta_id_div' | |
* ); | |
* any where on your theme, to hide/show any metabox or div element in admin menu for page or post edit. | |
* | |
* @author Digvijay Naruka | |
* @version 1.0 10/12/2017 | |
*/ | |
add_action('mythemeprefix_hide_meta_for_categories', 'mythemeprefix_hide_metabox', 10, 2 ); | |
/** | |
* Add custom action hook | |
* | |
* Do the custom hide_metabox action hook with parameters. | |
* | |
* @method custom_function function() use($content) contains the echo of the content(javascript) | |
* | |
* @param $cat_name array array of category names | |
* @param $div_id string id of the div element. | |
* @global $post | |
*/ | |
function mythemeprefix_hide_metabox($cat_names, $div_id){ | |
global $post; | |
// check if the metabox is for the correct post_type | |
if('post' != $post->post_type) | |
return; | |
//array to store category ids | |
$args = array(); | |
// loop through each category names and get the id. | |
foreach ($cat_names as $cat_name) { | |
$args[] = mythemeprefix_get_category_id_by_name($cat_name); | |
} | |
// call the function that contains the JQuery for hiding elements | |
$custom_function= mythemeprefix_hide_metabox_on_categories($div_id,$args); | |
//Add the javascript from the function to the footer of the admin menu. | |
add_action('admin_footer', $custom_function); | |
} | |
/** | |
* Show/Hide Metabox (or any other div ) for only Specific Categories during category selection. | |
* | |
* hide_metabox action with arguments. | |
* | |
* Version 1.1 : added dynamic div_id and category parameteres and break from each() loop | |
* after the first category is found. | |
* | |
* @param $cat_name array array of category names | |
* @param $div_id string id of the div element. | |
* @author Dimas Begunoff | |
* @verion 1.0 | |
* Original available at | |
* @link https://www.youtube.com/watch?v=hv1o6NrINu8&t=2s | |
* | |
* @since Version 1.1 | |
* | |
*/ | |
function mythemeprefix_hide_metabox_on_categories($div_id,$args){ | |
$content = '<script type="text/javascript">'; | |
$content .=' jQuery(document).ready(function($) {'; | |
$content .=' function mythemeprefix_hide_metabox_js(){'; | |
$content .=' $("#'.$div_id.'").hide();'; | |
$content .=" $('#categorychecklist input[type=".'"checkbox"]'."'".').each(function(i,e){'; | |
$content .=' var id = $(this).attr("id").match(/-([0-9]*)$/i);'; | |
$content .=' id = (id && id[1]) ? parseInt(id[1]) : null ;'; | |
$content .=' if ($.inArray(id, '. json_encode($args) .') > -1 && $(this).is(":checked") ){'; | |
$content .=' $("#'.$div_id.'").show();'; | |
$content .=' return false;'; | |
$content .=' }'; | |
$content .=' });'; | |
$content .=' }'; | |
$content .=" $('#categorychecklist input[type=".'"checkbox"]'."'".').live("click", mythemeprefix_hide_metabox_js);'; | |
$content .=" mythemeprefix_hide_metabox_js();"; | |
$content .=" });"; | |
$content .="</script>"; | |
$$return_echoed_content = function() use($content) {echo $content;}; | |
return $$return_echoed_content; | |
} | |
/** | |
* Get Category Id by name function | |
* | |
* @return string | |
*/ | |
function mythemeprefix_get_category_id_by_name($cat_name){ | |
$term = get_term_by('name', $cat_name, 'category'); | |
return $term->term_id; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment