• Looking for a solution for WordPress Admin where fields such as Title, Excerpt, Category are required before being able to publish. Warning message or alert displaying what is missing because it is required. I have found old solutions that work for classic editor, but they do not work with the block editor, and ones that do work for block editor do not work for classic editor . Have not found anything out there that works for both. Looking to plug in some code in the functions.php file. Any help would be appreciated for a jr dev here in WordPress.

    In my functions.php file

    This works for classic editor only

        /** ADD Validation for title */
        function force_post_title_init()
    {
        wp_enqueue_script('jquery');
    }
    function force_post_title()
    {
        echo "<script type='text/javascript'>\n";
        echo "
      jQuery('#publish').click(function(){
            var testervar = jQuery('[id^=\"titlediv\"]')
            .find('#title');
            if (testervar.val().length < 1)
            {
                jQuery('[id^=\"titlediv\"]').css('border', '2px solid red');
                alert('Post title is required');
                return false;
            }
        });
      ";
        echo "</script>\n";
    }
    add_action('admin_init', 'force_post_title_init');
    add_action('edit_form_advanced', 'force_post_title');
    add_action('edit_page_form', 'force_post_title');

    This works on classic editor but on block editor just refreshes back to the same post in draft.

    /**
     * Checks for empty post title, if empty sets the post status to draft
     *
     * @param $data
     * @param $postarr
     *
     * @return array
     */
    function abc_check_post_title( $data, $postarr ) {
        if ( is_array( $data ) && 'publish' == $data['post_status'] && empty( $data['post_title'] ) ) {
            $data['post_status'] = 'draft';
            update_option( 'abc_post_error', 'empty_title' );
        }
    
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'abc_check_post_title', 10, 2 );
    
    /**
     * If the post title was empty, do not show post published message
     */
    add_filter( 'post_updated_messages', 'abc_remove_all_messages' );
    
    function abc_remove_all_messages( $messages ) {
        if ( get_option( 'abc_post_error' ) ) {
            return array();
        } else {
            return $messages;
        }
    }
    
    /**
     * Show admin notice for empty post title
     */
    add_action( 'admin_notices', 'abc_show_error' );
    function abc_show_error() {
        $screen = get_current_screen();
        if ( $screen->id != 'post' ) {
            return;
        }
        if ( ! get_option( 'abc_post_error' ) ) {
            return;
        }
        echo '<div class="error"><p>' . esc_html__( "You need to enter a Post Title in order to publish it.", "wse" ) . '</p></div>';
        delete_option( 'abc_post_error' );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    @mbavosa – we have hijacked you topic for moderation purposes, apologies.

    To answer your question, in order to display warnings prior to publishing, you need jQuery or JavaScript to intercept the publish click action and check if the required fields have values or not. Because the different editors use different elements the same code cannot work for both.

    If you really need to enforce the required fields, you cannot rely upon client side scripts. You must backup what the scripts check for by checking again server side. Here you could use the ‘wp_insert_post_data’ filter to verify required fields. Unfortunately there is no elegant way to return a warning from here. You could simply call wp_die() with a message to go back and supply missing information. It’s crude but effective. A more elegant response is feasible but takes some creative coding.

    @aamirkhatri – thanks for understanding. If you’re willing to help through communication on the forums and making your effort publicly available, you’re more than welcome to do so. I’m sure others would appreciate the effort. If that’s too much to ask, that’s OK too. We’re all volunteers here and each as their limits of what they can contribute. Any contribution that complies with our guidelines, no matter how small, is welcome.

    Thread Starter devSoupGuy61

    (@mbavosa)

    @bcworkz thank you for your response. So how would you write a function for functions.php so that any post missing a title cannot be published and will set to draft using the wp_die() to display a message?

    Moderator bcworkz

    (@bcworkz)

    Your existing ‘wp_insert_post_data’ callback would still be effective. The problem you’re having is more in that a message back to the user of the block editor does not happen. I don’t have much experience with the block editor and don’t know how its messaging works. I imagine it involves a particular kind of API response. You can alter the response with the ‘rest_post_dispatch’ filter. I don’t know what sort of response would be meaningful though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Required/Mandatory fields in WordPress Admin’ is closed to new replies.