Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

<?php 
/* 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 */
if(empty($_POST['leadcandidate'])){
    $nersi = ($meta_values[_dtm_lead_candidate][0]);
}
else {
    $nersi = $_POST['leadcandidate'];
}

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

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 at 19:59

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.