I'm trying to make this php code run in a post. Using a plugin to enable the php to run.

Problem is I can't seem to get it to work no matter how I structure the statements :/

I'm sure it's something simple but I'm just not familiar enough with php yet to figure it out.

Any help would be greatly appreciated :)

[insert_php]if(!get_post_meta(get_the_ID()),'buy_status', true);
echo "<h5 style="text-align: center;display: block;">";
echo "Sorry, this piece is sold.";
echo "<h5 style="text-align: center;display: none;">";
  • Hi James. Questions about third party plugins and themes are off-topic here on WPSE, so it would be best to ask in the forum provided by the developer of the plugiin. Side note, allowing PHP to run in a post is not a good practice. It would be better to create a [shortcode] for this purpose. – Dave Romsey Dec 14 '16 at 20:58
  • Hi Dave, the question is about how to structure the php statements. The problem is the same for me even in a shortcode. If you read the question, I'm not asking how to make a theme or plugin work :) I'm asking about structuring the php so it works. Please read the questions in context. – James Dec 14 '16 at 21:02
  • Okay, but vanilla PHP questions are still off-topic here on WPSE. :-) I've added a solution which should act as a decent starting point and it doesn't rely on needing to run PHP in the content area. – Dave Romsey Dec 14 '16 at 21:22
  • What is a vanilla question?? Are you saying this is not a place for newbies to ask or learn?. – James Dec 14 '16 at 21:25
  • 1
    Inserting PHP code into content is a recipe for disaster. Plugins that add a way to put PHP code in the admin area like this are security disasters, massive exploit holes just waiting to be used, and encourage a method of development that very quickly leads to massive problems ( e.g. you'll run into major problems the moment you try to do simple things with the WordPress APIs, such as calling get_post_meta, etc ) – Tom J Nowell Dec 14 '16 at 21:25

Here's a solution using a [shortcode] which doesn't rely on running PHP inside of the content area. Here are the docs for get_post_meta() which show how that function should be called.

add_shortcode( 'buy_status', 'wpse249289_buy_status' );
function wpse249289_buy_status( $atts ) {
    $output = '';
    if ( ! get_post_meta( get_the_ID(), 'buy_status', true ) ) {
        // Post meta value for key buy_status is false
        $output .= '<h5 style="text-align: center;display: block;">';
        $output .= 'Sorry, this piece is sold.';
        $output .= '</h5>';
    } else {
        // Post meta value for key buy_status is not false
        $output .= '<h5 style="text-align: center;display: none;"></h5>'; // Not sure why you'd want to output anything

    return $output;
| improve this answer | |
  • Thanks so much Dave! I did read the docs but did not see any reference to the !. In finding what hat meant, it apparently inverts the statement. Meaning it looks for false instead of true I suppose. Why do that? – James Dec 14 '16 at 21:24
  • You got it, James, and yes you are correct. ! is the logical not operator. Often times, code will be structured so that the false conditions are checked first before the true conditions. This code could easily be rewritten to be the other way around though. – Dave Romsey Dec 14 '16 at 21:30
  • Okay, just wondering. So tell me, I just read over the types of topics that can be queried here (again). I am missing why this question is not something to be asked here. It applies to Wordpress in that I am trying to understand how to make php dynamically write HTML into a post. I am not a php programmer. I write code. Sometimes I get it right, sometimes I don't. I came here to get help with understanding what I did wrong. Dave provided that and I very much appreciate it. So, is the question inappropriate or not?? We can only learn from those who already know. – James Dec 14 '16 at 21:38
  • In my opinion, the question is off topic, but it's on the border line. That's just my opinion though. if(!get_post_meta(get_the_ID()),'buy_status', true); should have produced a syntax error. The PHP in the content plugin might have been masking that. Don't sweat it though, just keep doing your thing. It's always great to help folks that wan't to learn. If this solution solved your issue, please click the check mark to close it out. – Dave Romsey Dec 14 '16 at 21:44
  • One last question regarding the value of the post_meta. I dynamically create the buy_status key and the value. Currently, it is created with a function when I receive an IPN from PayPal. I set the value to 1, meaning true. The key doesn't exist until a transaction triggers its creation. Should I set it to 0 for false? Will the shortcode function hiccup if no key is present at all? Thanks :) – James Dec 14 '16 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.