0

After the save_post action runs, I'm attempting to also use the admin_notices action and it doesn't work. What am I doing wrong?

If I move the admin_notices action into the constructor, it works fine (of course it shows for every page in the admin which isn't desired). I'm assuming it must be the logical sequence of the hooks or the nesting itself?

Code sample:

if (!class_exists('CPTToPDF')) {

  // FPDF library
  require_once(plugin_dir_path(__FILE__) . 'fpdf182/fpdf.php');

  class CPTToPDF {
    private $pdf;

    public function __construct() {
      add_action('save_post', array($this, 'render_to_pdf'));
    }

    public function render_to_pdf() {
      //die('Render to PDF running...');
      $this->pdf = new FPDF();
      add_action('admin_notices', array($this, 'admin_notice__success'));
    }

    public function admin_notice__success() {
      //die('Admin notice running...');
      echo '<div class="notice notice-success is-dismissable">CPTToPDF: Saved post.</div>';
    }

    public function admin_notice__error() {
      echo '<div class="notice notice-error is-dismissable">CPTToPDF: Did not save post.</div>';
    }
  }

}

I placed the initial hook into save_post in the plugin's constructor method. That works fine (if I uncomment the die('Render to PDF running...'); line it does die and display that message, so I know my callback works.

However, this second action/hook doesn't fire properly inside of the render_to_pdf callback: add_action('admin_notices', array($this, 'admin_notice__success'));

Even if I uncomment the die('Admin notice running...'); I get no output when a post is saved and new page is reloaded (other than the default "Page Updated. View Page" admin notice). So the nested action doesn't seem to be working and I'm not sure why.

2
  • 2
    admin_notices fires when the page loads - so it's far too late to hook into it when you save.
    – vancoder
    Jun 10, 2020 at 18:20
  • Oh! Well that would make sense. So, if my intention is to add a notice after the page has been Created or Updated, which hook makes most sense? I still need it to be triggered by that initial save_post hook. Jun 10, 2020 at 18:36

1 Answer 1

1

You need to send your notice message - or probably a key that refers to a specific standard notice message - over the URL to the next page.

I'd advise encapsulating this in its own function. Something like...

public function render_to_pdf() {
    // ... whatever you need to do
      my_trigger_notice( 1 ); // 1 here would be a key that refers to a particular message, defined elsewhere (and not shown here)
    }

Then add your new function in functions.php (or an admin.php file):

function my_trigger_notice( $key = '' ) {
    add_filter(
        'redirect_post_location',
        function ( $location ) use ( $key ) {
            $key = sanitize_text_field( $key );

            return add_query_arg( array( 'notice_key' => rawurlencode( sanitize_key( $key ) ) ), $location );
        }
    );
}

Now when the page redirects, it should append your notice key on the URL, enabling the next page to "catch it" on the admin_notice hook (also set in functions.php or admin.php):

function my_admin_notices() {
    if ( ! isset( $_GET['notice_key'] ) ) {
        return;
    }
    $notice_key  = wp_unslash( sanitize_text_field( $_GET['notice_key'] ) );
    $all_notices = [
        1 => 'some notice',
        2 => 'some other notice',
    ]
    if ( empty( $all_notices[ $notice_key ] ) ) {
        return;
    }
        ?>
    <div class="error">
        <p>
            <?php echo esc_html( $all_notices[ $notice_key ] ); ?>
        </p>
    </div>
    <?php
    }

add_action( 'admin_notices', 'my_admin_notices' );
2
  • Thanks, I'll give this a look. Everything has been abstracted out from functions.php into a plugin, but I get the gist. Jun 10, 2020 at 19:52
  • missing a semicolon after array declaration;
    – infomasud
    Feb 28 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.