Dies ist das erste Mal, dass ich ein Plugin für WordPress erstelle und ich versuche, ein Plugin für das Front-End-Formular für eingeloggte Benutzer zu erstellen. Bisher erscheint das Front-End-Formular auf einer Seite mit einem Shortcode [write-here]
und ich überprüfe, ob Daten in die DB gelangen, aber es gibt einige Dinge, die nicht richtig funktionieren.
post_author
erhält keinen Wert (Gemäß diesem Codex wird der aktuelle Benutzer als Standard festgelegt, aber kein Wert in die DB)
Keine Rückgabe $post_id
. Ich kann den Post-ID-Rückgabewert nicht erhalten, nachdem ich das Formular abgeschickt habe.
Redirect funktioniert nicht (Dies ist wahrscheinlich, weil wp_insert_post
nicht richtig funktioniert. Scheint, als ob nichts nach wp_insert_post
)
Hier ist der Code, den ich bisher habe.
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); // Define plug-in path define('WH_PATH', plugins_url() . '/write-here'); // Load css & js files function my_enqueued_assets() { wp_enqueue_style( 'write-here', WH_PATH . '/css/write-here.css' ); wp_enqueue_script( 'write-here', WH_PATH . '/js/write-here.js', array( 'jquery' ), '1.0', true ); } add_action( 'wp_enqueue_scripts', 'my_enqueued_assets' ); /* ** Add front end from in a Shortcode https://codex.wordpress.org/Function_Reference/add_shortcode */ function form_write_here(){ $postdate = date('Ymd H:i:s'); // Show only to logged in users if ( is_user_logged_in() ) { ?> $title, 'post_content' => $content, 'post_category' => array($cat), // Default empty. 'tags_input' => array($tags), // Default empty. 'post_status' => 'publish', // Choose: publish, preview, future, draft, etc. Default 'draft'. 'post_date' => $postdate, // The time post was made. 'post_date_gmt' => $postdate // The time post was made, in GMT. ); //save the new post and return its ID $post_id = wp_insert_post($new_post, $wp_error ); // this not working // redirect after submition wp_redirect( site_url()."?post=".$post_id); exit(); }
Fühlen Sie sich frei, Anmerkungen für irgendwelche Vorschläge auch zu machen. Vielen Dank!
Antwort für Leute aktualisieren, die dasselbe Problem wie ich haben könnten.
Ich wickelte Front-End-Formular und den Code, um das Datum aus dem Formular in functionen auszuführen und es begann zu arbeiten.
Ich habe auch zusätzliche functionen hinzugefügt, um CSS / JS-Dateien nur auf der Seite zu laden, auf der das Plugin verwendet wird. Zusammen mit Fehlermeldung function.
Hier ist der neue Code
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); // Define plug-in path define('WH_PATH', plugins_url() . '/write-here'); /* ** Register CSS & JS assets for plug in ------------------------------------------------------------------ */ // register our form css function write_here_register_assets() { wp_register_style('write-here', WH_PATH . '/css/write-here.css'); wp_register_script( 'write-here', WH_PATH . '/js/write-here.js', array( 'jquery' ), '1.0', true ); } add_action('init', 'write_here_register_assets'); // load our form css function write_here_print_assets() { global $write_here_load_assets; if ( !$write_here_load_assets ) return; wp_print_styles('write-here'); wp_print_scripts('write-here'); } add_action('wp_footer', 'write_here_print_assets'); /* ** Add a shortcode for front end form https://codex.wordpress.org/Function_Reference/add_shortcode */ function form_write_here(){ // Load CSS & JS files global $write_here_load_assets; $write_here_load_assets = true; // Show only to logged in users if ( is_user_logged_in() ) { $output = write_here_form(); return $output; }else{ echo 'Please Sign in to continue...'; } } add_shortcode('write-here', 'form_write_here'); /* ** Front end From ------------------------------------------------------------------ */ function write_here_form(){ ob_start(); $postdate = date('Ymd H:i:s'); ?> < ?php write_here_show_error_messages(); ?> < ?php return ob_get_clean(); } /* ** Process data from front end form http://codex.wordpress.org/Function_Reference/wp_insert_post */ function write_here_add_new_post() { if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "write_here_new_post") { $title = wp_strip_all_tags($_POST['title']); $content = $_POST['content']; $postdate = $_POST['date']; $tags = $_POST['post_tags']; $cat = $_POST['cat']; // Do some minor form validation to make sure there is content if ($title == '') { write_here_errors()->add('title_not_vaild', __('Title not valid')); } if ($content == '') { write_here_errors()->add('content_not_vaild', __('Content not valid')); return false; } if (!$postdate) { $postdate = date('Ymd H:i:s'); } // Add the content of the form to $post as an array $new_post = array( 'post_title' => $title, 'post_content' => $content, 'post_category' => array($cat), // Default empty. 'tags_input' => array($tags), // Default empty. 'post_status' => 'publish', // Choose: publish, preview, future, draft, etc. Default 'draft'. 'post_date' => $postdate, // The time post was made. 'post_date_gmt' => $postdate // The time post was made, in GMT. ); $errors = write_here_errors()->get_error_messages(); // only create post if there are no errors if(empty($errors)) { //save the new post and return its ID $post_id = wp_insert_post($new_post); if($post_id) { // This will redirect you to the newly created post (Using GUID) $post = get_post($post_id); wp_redirect($post->guid); exit(); } } } } add_action('init', 'write_here_add_new_post'); // used for tracking error messages function write_here_errors(){ static $wp_error; // Will hold global variable safely return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null)); } // displays error messages from form submissions function write_here_show_error_messages() { if($codes = write_here_errors()->get_error_codes()) { echo ''; // Loop error codes and display errors foreach($codes as $code){ $message = write_here_errors()->get_error_message($code); echo '' . __('Error') . ': ' . $message . '
'; } echo ''; } }
Bitte zögern Sie nicht, Kommentare für irgendwelche Vorschläge zu machen.