Advertisement
Alhadis

WordPress Contact Form 7 - Editor Shortcut

Dec 30th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1. <?php
  2.  
  3. #   Allow a quicker means of editing the page's Contact Form 7 entry.
  4. if(defined('WPCF7_VERSION')){
  5.  
  6.     add_action('admin_bar_menu', function($menu){
  7.         global $post, $wpdb, $current_screen;
  8.  
  9.  
  10.         #   In admin panel: check if we're editing an existing CF7 entry.
  11.         if(is_admin() && @('toplevel_page_wpcf7' === $GLOBALS['current_screen']->id)){
  12.  
  13.             if($page = $wpdb->get_row($wpdb->prepare('SELECT * FROM '.$wpdb->posts.' WHERE (post_type = \'page\' OR post_type = \'post\') AND post_content LIKE \'%%[contact-form-7 id="%d"%%\'', $_GET['post']))){
  14.                 $type   =   get_post_type_object($page->post_type);
  15.                
  16.                 $menu->add_node(array(
  17.                     'id'    =>  'edit-wpcf7-page',
  18.                     'title' =>  $type->labels->edit_item,
  19.                     'href'  =>  get_edit_post_link($page->ID),
  20.                     'meta'  =>  array('class' => 'ab-icon-edit', 'title' => 'Edit the page using this contact form')
  21.                 ));
  22.             }
  23.             return;
  24.         }
  25.  
  26.  
  27.         #   Sanity check: bail if there's no $post global available.
  28.         if(!$post) return;
  29.  
  30.  
  31.         #   Post's content contains at least one CF7 shortcode; parse it and grab its ID.
  32.         if(preg_match('#\[contact-form-7[^\]]+id="(\d+)"[^\]]*\]#', $post->post_content, $matches)){
  33.             $id =   $matches[1];
  34.  
  35.             $menu->add_node(array(
  36.                 'id'    =>  'edit-wpcf7',
  37.                 'title' =>  'Contact Form',
  38.                 'href'  =>  admin_url('admin.php?page=wpcf7&post=' . $id . '&action=edit'),
  39.                 'meta'  =>  array('class' => 'ab-icon-email')
  40.             ));
  41.         }
  42.     }, 90);
  43.  
  44.  
  45.     #   DOM juice for adding icons to inserted admin-bar links.
  46.     add_action('wp_after_admin_bar_render', function(){
  47.         ?>
  48.         <style type="text/css">.ab-icon-email > a::before{content: "\F466"; top: 2px;}.ab-icon-edit > a::before{content:"\F464"; top: 2px;}</style>
  49.         <?php
  50.     });
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement