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' );