0
votes

I'm Trying to make a plugin that send emails with the post content to an email when admin check the check box. I found some codes and trying to do this

The plugin creates meta box with check box custom field.

Problem:

Not able to get custom field value in the plugin using get_post_meta to check whether to send the email or not Following is the code:

      add_action('admin_init','add_metabox_post_sidebar');
       add_action('save_post','save_metabox_post_sidebar');
      /*
        * Funtion to add a meta box to enable/disable the posts.
        */
      function add_metabox_post_sidebar()
       {
  add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");
        }

      function enable_sidebar_posts(){
         global $post;
          $check=get_post_custom($post->ID );
         $checked_value = isset( $check['post_sidebar'] ) ? esc_attr( $check['post_sidebar'][0] ) : 
   'no';
   ?>

     <label for="post_sidebar">Enable Sidebar:</label>
      <input type="checkbox" name="post_sidebar" id="post_sidebar" <?php if($checked_value=="yes") 
{echo "checked=checked"; } ?> >

<?php
  }

 /*
  * Save the Enable/Disable sidebar meta box value
 */
function save_metabox_post_sidebar($post_id)
  {
   // Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;

$checked_value = isset( $_POST['post_sidebar'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'post_sidebar', $checked_value );

     }




   // Function that runs when a post is published
    function run_when_post_published( $post ) {

    $author = get_userdata($post->post_author);
   $category = get_the_category();
   $author_id = $post->post_author;
  $author = get_the_author_meta( 'nickname', $author_id ); 
   $title = get_the_title();
  $permalink = get_permalink();
  $featured = get_the_post_thumbnail_url();
  $email_subject = $title;
  $message = apply_filters( 'the_content', $post->post_content );
  $message2=wp_strip_all_tags($message);
 $message3= html_entity_decode($message2);

$headers = 'Manarty <[email protected]>';
    global $wp_query;
    $thePostID = $wp_query->post->ID;
$k=get_post_meta( $thePostID, 'post_sidebar', true);

  if(!empty($k) ) {

    $email = '[email protected]';
    wp_mail( $email, $email_subject, $message3 );
    



}

}


   // Makes sure email only gets sent the first time a post is published
 add_action('new_to_publish', 'run_when_post_published');        
   add_action('draft_to_publish', 'run_when_post_published');      
   add_action('pending_to_publish', 'run_when_post_published');
1
How did you create the custom field? Did you use ACF or create it in your PHP? - FluffyKitten
using the Function above .. i also tried with ACF but also was not able to get the value also tried with CMB2 with no success - Osama Mohammed
Note : using ACF or CMB2 framework i was able to get the value dispplayed in single.php easily put not in functions.php or plugin page - Osama Mohammed
It's easy using ACF - you just call the get_field function. Have you debugged this code to see if the problem is with retrieving the value or whether it is getting saved at all? - FluffyKitten
I tried of course and the value saved successfully but i can not get it in plugins only able to get it in single,php - Osama Mohammed

1 Answers

0
votes

It think your problem might be here:

$k=get_post_meta( $thePostID, 'post_sidebar', true);

if in fact you already had saved 'post_sidebar' correctly it means that you don't have the correct value of $thePostID

Instead of your code try this, it works for me:

global $post;
$currentPostObject = $post;
$currentPostObjectID = $currentPostObject->ID;