Force Category

Posted in: Newsmag
Post count: 104

So … i’m trying to add a function that forces the user to use Categories, so I’ve got this:

function force_post_categ_init()
{
wp_enqueue_script('jquery');
}
function force_post_categ()
{
echo "<script type='text/javascript'>\n";
echo "
jQuery('#publish').click(function()
{
var cats = jQuery('[id^=\"taxonomy\"]')
.find('.selectit')
.find('input');
category_selected=false;
for (counter=0; counter<cats.length; counter++)
{
if (cats.get(counter).checked==true)
{
category_selected=true;
break;
}
}
if(category_selected==false)
{
alert('You have not selected any category for the post. Please select post category.');
setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
return false;
}
});
";
echo "</script>\n";
}
add_action('admin_init', 'force_post_categ_init');
add_action('edit_form_advanced', 'force_post_categ');

but I need specific categories selected … like a region and a content related category.

Any ideas?

Post count: 104

Maybe … i can force it to have at least three categories selected … and an alert will pop up with “Have you selected a region in the category list?”

Post count: 6535

Hi

It seems that your code force the user to select at least one category for a post. To force the user to use only specific categories the simpler way would be to hide the categories that you don’t want to be used. You can inject some css and make vincible only the categories that you want to be used.
This could be one way but you can try other different methods.

Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
The forum ‘Newsmag’ is closed to new topics and replies.