Fügen Sie ein Meta zu einem Post hinzu, der von einem Frontend-Formular übergeben wurde

Ich verwende ein Frontend-Formular von @TheDeadMedic, um regelmäßige Posts zu veröffentlichen. Ich habe herausgefunden, wie man zu diesem Formular eine Dropdown-Liste für Kategorie und Standort (benutzerdefinierte Taxonomie) hinzufügt, wie man sie bereinigt und validiert, und schließlich, wie man den Beitrag mit dieser neuen Information speichert. Aber ich kann einen zweiten Standorteingabewert nicht als Post-Meta speichern . Ich kenne die function add_post_meta() , aber ich verstehe nicht, wie ich sie im Kontext des erwähnten Frontend-Formulars verwenden kann. Irgendeine Hilfe?

Mein individuelles Frontend-Formular:

 pluginPath = plugin_dir_path( __file__ ); $this->pluginUrl = plugins_url( '', __file__ ); add_action( 'wp_enqueue_scripts', array( $this, 'addStyles' ) ); add_shortcode( 'post_from_front', array( $this, 'shortcode' ) ); } function addStyles() { //wp_enqueue_style( 'submitform-style', "$this->pluginUrl/submitfromfront.css" ); } /** * Shortcodes should return data, NOT echo it. * * @return string */ function shortcode() { if ( ! current_user_can( 'publish_posts' ) ) return sprintf( '

Please login to post adverts.

', esc_url( wp_login_url( get_permalink() ) ) ); elseif ( $this->handleForm() ) return '

Nice one, post created!

'; else return $this->getForm(); } /** * Process the form and return true if post successfully created. * * @return bool */ function handleForm() { if ( ! $this->isFormSubmitted() ) return false; // http://php.net/manual/en/function.filter-input-array.php $data = filter_input_array( INPUT_POST, array( 'postTitle' => FILTER_DEFAULT, 'postContent' => FILTER_DEFAULT, 'postCategory' => FILTER_DEFAULT, 'postLocation' => FILTER_DEFAULT, 'postLocation2' => FILTER_DEFAULT, )); $data = wp_unslash( $data ); $data = array_map( 'trim', $data ); // You might also want to more aggressively sanitize these fields // By default WordPress will handle it pretty well, based on the current user's "unfiltered_html" capability $data['postTitle'] = sanitize_text_field( $data['postTitle'] ); $data['postContent'] = wp_check_invalid_utf8( $data['postContent'] ); $data['postCategory'] = sanitize_text_field( $data['postCategory'] ); $data['postLocation'] = sanitize_text_field( $data['postLocation'] ); $data['postLocation2'] = sanitize_text_field( $data['postLocation2'] ); $this->data = $data; if ( ! $this->isNonceValid() ) $this->errors[] = 'Security check failed, please try again.'; if ( ! $data['postTitle'] ) $this->errors[] = 'Please enter a title.'; if ( ! $data['postContent'] ) $this->errors[] = 'Please enter the content.'; if ( ! $data['postCategory'] ) $this->errors[] = 'Please select a category.'; if ( ! $data['postLocation'] ) $this->errors[] = 'Please select a location.'; if ( ! $this->errors ) { $post_id = wp_insert_post( array( 'post_title' => $data['postTitle'], 'post_content' => $data['postContent'], 'post_category' => array( $data['postCategory'] ), 'tax_input' => array('loc' => array( $data['postLocation'] )), 'post_status' => 'publish', )); if ( ! $post_id ) $this->errors[] = 'Whoops, please try again.'; } else { $post_id = 0; } return ( bool ) $post_id; } /** * Use output buffering to *return* the form HTML, not echo it. * * @return string */ function getForm() { ob_start(); ?>
errors as $error ) : ?>

<input type="text" name="postTitle" id="postTitle" value="data['postTitle'] ) ) echo esc_attr( $this->data['postTitle'] ); ?>" />
data['postCategory'] ) && $this->data['postCategory'] == $category->cat_ID ) { $option = 'cat_ID . '" selected="selected">'; } else { $option = 'cat_ID . '">'; } $option .= $category->cat_name; $option .= ' ('.$category->category_count.')'; $option .= ''; echo $option; } ?>
data['postLocation'] ) && $this->data['postLocation'] == $category->term_id ) { $option = 'term_id . '" selected="selected">'; } else { $option = 'term_id . '">'; } $option .= $category->name; $option .= ' ('.$category->count.')'; $option .= ''; echo $option; } ?>
<input type="text" name="postLocation2" id="postLocation2" value="data['postLocation2'] ) ) echo esc_attr( $this->data['postLocation2'] ); ?>" />
<?php return ob_get_clean(); } /** * Has the form been submitted? * * @return bool */ function isFormSubmitted() { return isset( $_POST['submitForm'] ); } /** * Is the nonce field valid? * * @return bool */ function isNonceValid() { return isset( $_POST[ self::NONCE_FIELD ] ) && wp_verify_nonce( $_POST[ self::NONCE_FIELD ], self::NONCE_VALUE ); } } new WPSE_Submit_From_Front;

Solutions Collecting From Web of "Fügen Sie ein Meta zu einem Post hinzu, der von einem Frontend-Formular übergeben wurde"