Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I maintain a plugin which generates a PDF of a page or post when an icon or link is clicked. PDFs may be cached, and as such, when a page or post is edited, we refresh the cache by regenerating the PDF.

In the admin section of the plugin, we do the following:

if (is_admin()) {
add_action('admin_init', array(&$this, 'on_admin_init'));
add_action('admin_menu', array(&$this, 'on_admin_menu'));
add_filter("plugin_action_links_" . WPPTOPDFENH_BASENAME, array(&$this, 'action_links'));
register_activation_hook(WPPTOPDFENH_BASENAME, array(&$this, 'on_activate'));
add_action('pre_post_update', array(&$this, 'on_pre_post_update'));
add_action('post_updated', array(&$this, 'generate_pdf_file'));
} else {
add_action('wp', array(&$this, 'generate_pdf'));
add_filter('the_content', array(&$this, 'add_button'));

}

Oddly, when saving menus, we get triggered to refresh the cache, which leads to a:

Warning: Creating default object from empty value in /home/{site root}/public_html/wp-content/plugins/wp-post-to-pdf-enhanced/wp-post-to-pdf-enhanced.php on line ###.

I've implemented a rather ugly (I think) workaround, as I am at a loss to figure out why we get triggered when saving on the menu page:

if (is_admin()) {
add_action('admin_init', array(&$this, 'on_admin_init'));
add_action('admin_menu', array(&$this, 'on_admin_menu'));
add_filter("plugin_action_links_" . WPPTOPDFENH_BASENAME, array(&$this, 'action_links'));
register_activation_hook(WPPTOPDFENH_BASENAME, array(&$this, 'on_activate'));

   // Ugly workaround to keep from firing when navigation menus are saved
   $screen = get_current_screen();
   if ( !empty( $screen ) && $screen->id != 'nav-menus' ) {
       add_action('pre_post_update', array(&$this, 'on_pre_post_update'));
       add_action('post_updated', array(&$this, 'generate_pdf_file'));
   }
} else {
add_action('wp', array(&$this, 'generate_pdf'));
add_filter('the_content', array(&$this, 'add_button'));
}

Because screen.php is not yet loaded, earlier in the file we include it so that get_current_screen() is available:

require_once(ABSPATH . 'wp-admin/includes/screen.php');

Surely, there must be a more elegant solution?

TIA

Lewis

share|improve this question
    
Is the warning happening during generate_pdf_file? –  czerspalace Mar 8 at 6:13
    
If so, nav-menu items are a post type, so what might work is checking post type inside the function –  czerspalace Mar 8 at 6:24
    
Sorry for the late reply, and thanks for following up! –  LewisR Mar 11 at 7:16
    
@czerspalace (Hit <Enter> too soon, and my edit timed out...) Indeed, the warning occurs when we try to generate a PDF without the requisite data (which certainly makes sense, given the timing of the request). Check the post type inside the function... Hmmm... Need to figure out, then, how to do that at that particular point in the execution (get_post_type( $post ) and then match that to the allowed post types as set in the plugin's options?). –  LewisR Mar 11 at 7:25
    
@Dylan: Thanks for the typo fix in the subject. I swear, as I'm getting older, my fingers are getting fatter! –  LewisR Mar 11 at 7:27
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.