• Resolved cyberlp23

    (@cyberlp23)


    Hello,

    I’m trying to trigger an action if, when a user tries to publish a post, no category is selected:
    – It could either be just an alert message (and the post is still published);
    – Or actually alter message + preventing the post from being published.

    Searching on this forum, I’ve found the code:

    jQuery('#submitdiv').on('click','#publish',function(e){
                var $checked = jQuery('#category-all li input:checked');
                if ( $checked.length <= 0 ) { //Checks if cat is selected
                alert("Please Select atleast one category");
                return false;
                }else{ //Else continue
                    return true;
                }
                });

    But I have no idea where to put it.

    Thanks!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter cyberlp23

    (@cyberlp23)

    I have tried using the publish_post hook, but apparently there is an error on line:
    jQuery('#submitdiv').on('click','#publish',function(e){

    Chris Brown

    (@shortwavewebsolutions)

    Hey do you have a link to the forum post where you found it?

    I can have a look tomorrow for you (its late night here in NZ!)

    Thread Starter cyberlp23

    (@cyberlp23)

    I double checked and it wasn’t in these forums, it was there:
    http://w3facility.org/question/prevent-post-from-being-published-if-no-category-selected/

    Thanks for your help!

    Chris Brown

    (@shortwavewebsolutions)

    Hey

    OK I tested the JS you posted, it was fine.

    So I guess the issue must be somewhere else. What error did you get from line jQuery('#submitdiv').on('click','#publish',function(e){?

    Anyway, as I don’t know how (or where) you’re calling the JS, I have written you a very basic plugin to do what you need. You can download it at: https://github.com/Shortwave-Web-Solutions-Ltd/force-js-on-wp-admin-post-page

    Just edit the force-js-on-wp-admin-post-page.js file, save your JS code in there then it will throw up your alert on both the add post and edit post screens (just incase someone removes all existing categories or edits a post that doesn’t have categories after you’ve implemented the plugin).

    That should solve your problem, let me know if you have any more issues with it.

    Cheers
    Chris

    Thread Starter cyberlp23

    (@cyberlp23)

    Thanks for the script. I’ve added it to my functions.php file used in the child theme.
    There’s no error now, but nothing happens, no javascript reaction at all… (publishing a new page or editing an old one)

    Chris Brown

    (@shortwavewebsolutions)

    Did you try the Plugin? Did that work?

    Thread Starter cyberlp23

    (@cyberlp23)

    The plugin does work, yes, thanks!

    I was just wondering how to include it in functions.php instead of using a plugin?

    Chris Brown

    (@shortwavewebsolutions)

    Ah that’s good just wanted to make sure there was nothing weird happening with the Javascript.

    OK so can you try replacing the jquery code with just a simple alert box that pops up on page load?

    Chris Brown

    (@shortwavewebsolutions)

    Sorry meant to say, use the alert box in your theme we can do away with the Plugin now we know the js does work on your page

    Thread Starter cyberlp23

    (@cyberlp23)

    I just managed to add the code to functions.php of the child theme. It does work, and no plugin required. Thanks for helping mate!

    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    
    function my_enqueue($hook) {
    	$pages = array('post.php','post-new.php');
    	if (in_array($hook, $pages)) {
    		wp_enqueue_script( 'my_custom_script', get_stylesheet_directory_uri() . '/force-js-on-wp-admin-post-page.js',array(),FALSE,TRUE);
    	} else {
    		return;
    	}
    }
    Thread Starter cyberlp23

    (@cyberlp23)

    I do have another problem though: the script is applied even for non-articles posts like the portfolio or some plugins using the post.php file.
    How can I restrict it only for the publication of articles?
    Thanks!

    Chris Brown

    (@shortwavewebsolutions)

    How do you define ‘non-articles’? Are they setup as custom post types, in a particular category or have particular tags?

    Thread Starter cyberlp23

    (@cyberlp23)

    Yes, custom post types, including the wordpress portfolio.
    I was wondering how I could restrict the script to be applied only to articles belonging to certain categories.

    Thread Starter cyberlp23

    (@cyberlp23)

    (by categories, I mean, not custom post types, for example)

    Chris Brown

    (@shortwavewebsolutions)

    Without seeing your code, my guess would be having a play with the $pages array…

Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘Prevent posting when no category is selected’ is closed to new replies.