Support » Plugin: Gutenberg » How to inject own messages into Gutenberg’s responses?

  • Resolved MarkRH

    (@markrh)


    I’ve added some code to add messages after a post is published. With the Classic Editor I get: https://i.imgur.com/oRhc4FQ.png

    With Gutenberg I get: https://i.imgur.com/hKnHWdB.png

    This works with the Classic Editor:

    function mrh_updated_messages( $messages ) {
       global $post, $post_ID;
       $messages["post"][1] = sprintf( __('Page updated. <a href="%s">View page</a>'), esc_url( get_permalink($post_ID) ) ).$_SESSION['admin_msg1'];
       $messages["post"][6] = sprintf( __('Post published. <a href="%s">View post</a>'), esc_url( get_permalink($post_ID) ) ).$_SESSION['admin_msg6'];
       return $messages;
    }
    
    add_filter('post_updated_messages', 'mrh_updated_messages');

    How do I achieve this with Gutenberg? Oh, the session variables are created in add_action('publish_post', 'mrh_notify_twitter', 99);.

    Actually, looks like the information generated from add_action('in_admin_footer', 'memory_stats'); also does not appear when using Gutenberg. Hmmm.. How can I get anything to appear anywhere?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Well.. looking at the source code, add_action('in_admin_footer', 'memory_stats'); does output its information:

    
    <div id="wpfooter" role="contentinfo">
    	<p style="clear: both;">Memory used: 12,058,624 bytes.<br />Peak Memory used: 12,582,912 bytes.</p>	<p id="footer-left" class="alignleft">
    		<span id="footer-thankyou">Thank you for creating with <a href="https://wordpress.org/">WordPress</a>.</span>	</p>
    	<p id="footer-upgrade" class="alignright">
    		Version 4.9.8	</p>
    	<div class="clear"></div>
    </div>

    But, I can’t see it anywhere on the page. Hidden or something. Oh, this CSS is hiding it:

    
    body.gutenberg-editor-page #wpbody-content > div:not(.gutenberg):not(#screen-meta), body.gutenberg-editor-page #wpfooter {
    	display: none;
    }

    Well, I can undo that if I want.

    Moderator Marius L. J.

    (@clorith)

    Hiya,

    We were discussing this the other day that Gutenberg could do with an API for adding notifications, that’s still in the planning phase though, so nothing solid to show to unfortunately.

    What you may wish to do, since Gutenberg doesn’t refresh the page like the old editor did, is look at hooking into the save post feature in JavaScript, possibly looking at how the current save post notice has been implemented to know what styles etc are supported/encouraged.

    OK. Thanks. I’ll see if I can figure something out. If the plugin gets updated, won’t it delete anything I do with the JavaScript?

    • This reply was modified 2 years, 3 months ago by t-p.
    • This reply was modified 2 years, 3 months ago by MarkRH.
    Moderator Marius L. J.

    (@clorith)

    You would be creating this in your own code, not modifying Gutenbergs code πŸ™‚

    Yeah I know. πŸ™‚ I just haven’t been able to find where yet. One thing I tried resulted in Publishing failing. Tried to generate a JavaScript alert box using PHP echo. Never messed with JQuery stuff before. Hmmm..

    Moderator Marius L. J.

    (@clorith)

    Yup, you’ll need it all to be JavaScript.

    This is just off the top of my head, so it’s not tested, but it will at the very least get you started:

    
    wp.hooks.addAction(
        'editor.effects.postUpdated', 
        'my-plugin/save-action-event-namespace',
        function( newPost, isAutosave ) {
            if ( true === isAutosave ) {
                return; // Don't do anything during autosaves.
            }
            // Do your stuff here.
            consoe.dir( newPost );
        }
    );
    
    Scott Fennell

    (@scofennellgmailcom)

    Could you clarify the purpose and syntax of your line 3 there?

    'my-plugin/save-action-event-namespace',

    Moderator Marius L. J.

    (@clorith)

    That should be your own namespace, basically my-plugin is your plugin slug, and save-action-event-namespace is an identifier to the event you are triggering in your own plugin.

    Scott Fennell

    (@scofennellgmailcom)

    Thank you so much for the response!

    Does it do anything programmatically or is it just for debugging/readability?

    Moderator Marius L. J.

    (@clorith)

    I’m afraid that’s outside the scope of my knowledge of it, if you do find out I’d love to know though πŸ™‚

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to inject own messages into Gutenberg’s responses?’ is closed to new replies.