最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add notice text above Post Editor?

programmeradmin2浏览0评论

I'm trying to add a notice text that said "Please choose a category and add feature Image" text right above the editor so that they don't forget but I'm not sure how to do that so I would be really appreciate if I can get any help.

I've try every method I could find on the internet but it seems like they are a couple of years ago so it's not working for some reason.

I don't want any functionality, I just wanted to show a very simple text above the Title in the text editor so that they don't forget to include category and image.

I'm trying to add a notice text that said "Please choose a category and add feature Image" text right above the editor so that they don't forget but I'm not sure how to do that so I would be really appreciate if I can get any help.

I've try every method I could find on the internet but it seems like they are a couple of years ago so it's not working for some reason.

I don't want any functionality, I just wanted to show a very simple text above the Title in the text editor so that they don't forget to include category and image.

Share Improve this question edited Feb 24, 2022 at 16:14 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 6, 2022 at 20:36 ziicoziico 1311 bronze badge 1
  • are you using gutenberg or classic editor or some other editor all the same? – rudtek Commented Feb 7, 2022 at 4:38
Add a comment  | 

1 Answer 1

Reset to default 1

Assuming you're using gutenberg you need to use javascript to do this. Here's the steps I would take. In your child theme you'll need to add some code.

  1. Add a new directory "js".

  2. In that directory add a new file called "alerts.js"

  3. In that file add this code: (this is the actual warning note)

    ( function( wp ) {
     wp.data.dispatch('core/notices').createNotice(
         'warning', // Can be one of: success, info, warning, error.
         'Please remember to add a featured image and choose a category.', // Text string to display.
         {
             isDismissible: true, // Whether the user can dismiss the notice.
             // Any actions the user can perform.
         }
     );
     } )( window.wp );
    
  4. Now tell WordPress that you want to use that code by enqueuing the JS in the child theme's functions.php:

     function alerts_enqueue() {
         wp_enqueue_script('alerts-script', get_stylesheet_directory_uri() . '/js/alerts.js');
     }
     add_action( 'enqueue_block_editor_assets', 'alerts_enqueue' );
    

If you're using the classic editor or another block editor this will not work.

I'd also suggest just requiring the featured image and/or category selection if you really want the users abide by the "rules". :)

发布评论

评论列表(0)

  1. 暂无评论