Plugin Directory

Changeset 3265815


Ignore:
Timestamp:
04/02/2025 05:37:29 PM (12 months ago)
Author:
santoshtmp7
Message:

update setting, add more screenshot and blueprints

Location:
post-title-required
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • post-title-required/tags/1.0.0/include/check_setting.php

    r3265719 r3265815  
    4444 * ===================================================
    4545 */
    46 function ptreq_check_title_length_setting($post_id, $post, $update) {
    47     // Don't run during autosave or for non-public post types
    48     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    49     if (wp_is_post_revision($post_id)) return;
    50     //
    51     $character_limit = get_option('ptreq_character_limit', 100);
    52     $selected_post_types = get_option('ptreq_post_types', []);
     46function ptreq_check_title_length_setting($data, $postarr, $unsanitized_postarr) {
     47    try {
    5348
    54     // If no post type is selected, apply to all public post types
    55     if (empty($selected_post_types)) {
    56         $selected_post_types = array_keys(get_post_types(['public' => true]));
    57     }
     49        // Skip autosaves, revisions, and deletions
     50        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $data;
     51        if (wp_is_post_revision($postarr['ID'])) return $data;
     52        if ($data['post_status'] == 'trash' || $data['post_status'] == 'draft') {
     53            return $data;
     54        }
    5855
    59     // Check if the current post type is one of the selected types
    60     if (in_array($post->post_type, $selected_post_types)) {
    61         $title_length = mb_strlen($post->post_title);
     56        //
     57        $post_type = $data['post_type'];
     58        $character_limit = (int)get_option('ptreq_character_limit', 100);
     59        $selected_post_types = get_option('ptreq_post_types', []);
    6260
    63         // If the title is shorter than the required limit, prevent saving and show an error
    64         if ($title_length > $character_limit) {
    65             remove_action('save_post', 'ptreq_check_title_length_setting', 10);
    66             // Display error message
    67             wp_die(
    68                 sprintf(
    69                     'The title is too long! It must be at maximum %d characters long. Please correct it.',
    70                     esc_attr($character_limit)
    71                 ),
    72                 'Title Too long',
    73                 ['back_link' => true]
    74             );
    75         } else if (!$title_length) {
    76             wp_die(
    77                 sprintf(
    78                     'The title is required, with maximum %d characters long. ',
    79                     esc_attr($character_limit)
    80                 ),
    81                 'Title is empty',
    82                 ['back_link' => true]
    83             );
     61        // If no post type is selected, apply to all public post types
     62        if (empty($selected_post_types)) {
     63            $selected_post_types = array_keys(get_post_types(['public' => true]));
    8464        }
     65
     66        // Check if the current post type is one of the selected types
     67        if (in_array($post_type, $selected_post_types)) {
     68            $title_length = (int)mb_strlen(trim($data['post_title']));
     69
     70            // If the title is shorter than the required limit, prevent saving and show an error
     71            if ($title_length > $character_limit) {
     72                // Display error message
     73                wp_die(
     74                    sprintf(
     75                        'The title is too long! It must be at maximum %d characters long. Please correct it.',
     76                        esc_attr($character_limit)
     77                    ),
     78                    'Title Too long'
     79                );
     80            }
     81            if (!$title_length) {
     82                wp_die(__('Title is required.'));
     83            }
     84        }
     85        return $data;
     86    } catch (\Throwable $th) {
     87        error_log($th->getMessage());
    8588    }
    8689}
    87 add_action('save_post', 'ptreq_check_title_length_setting', 10, 3);
     90add_action('wp_insert_post_data', 'ptreq_check_title_length_setting', 10, 3);
  • post-title-required/tags/1.0.0/readme.txt

    r3265766 r3265815  
    2323
    2424== Screenshots ==
    25 1. screenshot-1.png
     251. Backend settings
     262. Quick Edit option
     273. Maximum character limit
    2628
    2729== Release ==
  • post-title-required/trunk/include/check_setting.php

    r3265713 r3265815  
    4444 * ===================================================
    4545 */
    46 function ptreq_check_title_length_setting($post_id, $post, $update) {
    47     // Don't run during autosave or for non-public post types
    48     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    49     if (wp_is_post_revision($post_id)) return;
    50     //
    51     $character_limit = get_option('ptreq_character_limit', 100);
    52     $selected_post_types = get_option('ptreq_post_types', []);
     46function ptreq_check_title_length_setting($data, $postarr, $unsanitized_postarr) {
     47    try {
    5348
    54     // If no post type is selected, apply to all public post types
    55     if (empty($selected_post_types)) {
    56         $selected_post_types = array_keys(get_post_types(['public' => true]));
    57     }
     49        // Skip autosaves, revisions, and deletions
     50        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $data;
     51        if (wp_is_post_revision($postarr['ID'])) return $data;
     52        if ($data['post_status'] == 'trash' || $data['post_status'] == 'draft') {
     53            return $data;
     54        }
    5855
    59     // Check if the current post type is one of the selected types
    60     if (in_array($post->post_type, $selected_post_types)) {
    61         $title_length = mb_strlen($post->post_title);
     56        //
     57        $post_type = $data['post_type'];
     58        $character_limit = (int)get_option('ptreq_character_limit', 100);
     59        $selected_post_types = get_option('ptreq_post_types', []);
    6260
    63         // If the title is shorter than the required limit, prevent saving and show an error
    64         if ($title_length > $character_limit) {
    65             remove_action('save_post', 'ptreq_check_title_length_setting', 10);
    66             // Display error message
    67             wp_die(
    68                 sprintf(
    69                     'The title is too long! It must be at maximum %d characters long. Please correct it.',
    70                     esc_attr($character_limit)
    71                 ),
    72                 'Title Too long',
    73                 ['back_link' => true]
    74             );
    75         } else if (!$title_length) {
    76             wp_die(
    77                 sprintf(
    78                     'The title is required, with maximum %d characters long. ',
    79                     esc_attr($character_limit)
    80                 ),
    81                 'Title is empty',
    82                 ['back_link' => true]
    83             );
     61        // If no post type is selected, apply to all public post types
     62        if (empty($selected_post_types)) {
     63            $selected_post_types = array_keys(get_post_types(['public' => true]));
    8464        }
     65
     66        // Check if the current post type is one of the selected types
     67        if (in_array($post_type, $selected_post_types)) {
     68            $title_length = (int)mb_strlen(trim($data['post_title']));
     69
     70            // If the title is shorter than the required limit, prevent saving and show an error
     71            if ($title_length > $character_limit) {
     72                // Display error message
     73                wp_die(
     74                    sprintf(
     75                        'The title is too long! It must be at maximum %d characters long. Please correct it.',
     76                        esc_attr($character_limit)
     77                    ),
     78                    'Title Too long'
     79                );
     80            }
     81            if (!$title_length) {
     82                wp_die(__('Title is required.'));
     83            }
     84        }
     85        return $data;
     86    } catch (\Throwable $th) {
     87        error_log($th->getMessage());
    8588    }
    8689}
    87 add_action('save_post', 'ptreq_check_title_length_setting', 10, 3);
     90add_action('wp_insert_post_data', 'ptreq_check_title_length_setting', 10, 3);
  • post-title-required/trunk/readme.txt

    r3265766 r3265815  
    2323
    2424== Screenshots ==
    25 1. screenshot-1.png
     251. Backend settings
     262. Quick Edit option
     273. Maximum character limit
    2628
    2729== Release ==
Note: See TracChangeset for help on using the changeset viewer.