Question

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.

Était-ce utile?

La solution

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' );
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top