1

I need to retrieve the meta value from db to the custom meta box I've created.

Here is my code.

 function wpl_owt_book_function( $book ) {

    define("_FILE_", "_FILE_");

    wp_nonce_field( basename(_FILE_), "wp_owt_cpt_nonce");

    echo "<label for='txtPublisherName'>Publisher Name</label>";
    $publisher_name = get_post_meta($post->ID, "book_publisher_name" , true);
    echo "<input type ='text' name = 'txtPublisherName' value = '<?php $publisher_name; ?>' placeholder = 'Publisher Name' />";
}

The result output from the text box makes place holder as <?php and outside the text box ") placeholder = 'Publisher Name' />"

Can anyone look into this matter and give me a solution please!

Have a great day!

3
  • 1
    Your last echo won't echo what you expect. You can't have opening and closing PHP tags inside an echo (which already is inside php tags). To concatenate a variable in a string, use: echo "something " . $foo . " something";. Commented Dec 30, 2019 at 10:44
  • Hi Magnus, Thanks for your quick response. Do you have any solution for that? Commented Dec 30, 2019 at 10:45
  • I added an example to the comment. I would also recommend reading up about how to use strings. This is one of those "PHP 101" knowledge you need to know. Commented Dec 30, 2019 at 10:46

2 Answers 2

2

You are already in PHP, so change this:

echo "<input type ='text' name = 'txtPublisherName' value = '<?php $publisher_name; ?>' placeholder = 'Publisher Name' />";

To this! (removing PHP tags)

echo "<input type ='text' name = 'txtPublisherName' value = '" . $publisher_name . "' placeholder = 'Publisher Name' />";
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Magnus. Thanks for your generous reply. I replaced it with your code. But the meta value hasn't retrieved to text box
Shouldn't $post->ID be $book->ID ? $post is not defined anywhere
Thanks Mate!. It worked. I changed $book into $post
0

You need to replace $post->ID with get_the_ID(). so, replace

get_post_meta($post->ID, "book_publisher_name" , true);

with

get_post_meta(get_the_ID(), "book_publisher_name" , true);

and also replace

echo "<input type ='text' name = 'txtPublisherName' value = '<?php $publisher_name; ?>' placeholder = 'Publisher Name' />";

with

echo "<input type ='text' name = 'txtPublisherName' value = '".$publisher_name."' placeholder = 'Publisher Name' />";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.