WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm just starting to learn php/js and would like some recommendations on the best way to setup my logic: Goal- Display the value in a custom metafield in the custom post type. If the user is the author of the page, display an edit button that will reveal an input field. This input should let the user(author) to update the custom field(metabox) from frontend.

The following code i wrote works, but seems sloppy, especially when since i plan to repeat it several times in the template for many custom fields. still just beginning to learn this.

/* check if POST 'leadcandidate' has no value and set var $nersi by custom field in database, or by POST if it has a positive value */
    $nersi = ($meta_values[_dtm_lead_candidate][0]);
else {
    $nersi = $_POST['leadcandidate'];

/* check if user is author of custom post type */
global $current_user;

if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
    echo '<button id=btn3>Edit me</button>';
    /* input field will be hidden and revealed on button click with JS */
    /* add $nersi as vlue of input so user starts from seeing what current value of field is */
    echo '<form method="post" action=""><input type="text" name="leadcandidate" value="' . $nersi . '"><input type="submit"></form>';
/* this updates the database that stores the metafield */
update_post_meta(get_the_ID(), '_dtm_lead_candidate', $_POST['leadcandidate']);
/* display the value of metafield (to everyone) */
<h3 id="leadc"><?php echo 'Lead Candidate:' . $nersi; ?></h3>
share|improve this question
Hi! :) What is your question here? Straight up code reviews are bit of grey area on this site. They are not outright considered out of scope, but they aren't too popular either. – Rarst Jun 15 '14 at 19:59

Your Answer


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

Browse other questions tagged or ask your own question.