Created
May 17, 2018 15:48
-
-
Save DavidPeralvarez/6a6d2ff6d189fb4f428d68911d5f231f to your computer and use it in GitHub Desktop.
WordPress Plugin Front End Posting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Front End Posting | |
Description: Create posts from the front end | |
Author: David Perálvarez | |
Version: 1.0 | |
Author URI: https://silicodevalley.com | |
*/ | |
// Front end submission form | |
function scv_submission_form(){ | |
if( !is_user_logged_in() ) return '<p>' . __( 'Para publicar un post, debes estar logueado.', 'scv' ) . '</p>'; | |
if( !is_page()) return '<p>' . __( 'Este shortcode sólo funciona dentro de páginas.', 'scv' ) . '</p>'; | |
ob_start(); | |
?> | |
<style> | |
#scv-submission-form input, | |
#scv-submission-form textarea{ | |
margin-bottom: 20px; | |
} | |
.scv-alert{ | |
padding: 10px; | |
border: 1px solid black; | |
} | |
.scv-alert.error{ | |
border-color: #f44250; | |
} | |
.scv-alert.success{ | |
border-color: #09ef71; | |
} | |
</style> | |
<?php do_action( 'scv_alerts' ); ?> | |
<form id="scv-submission-form" action="" method="post"> | |
<label for="post-title"><?php _e( 'Título del post', 'scv' ); ?></label> | |
<input name="post-title" id="post-title" type="text"> | |
<label for="post-content"><?php _e( 'Contenido del post', 'scv' ); ?></label> | |
<textarea name="post-content" id="post-content" rows="4" cols="50"></textarea> | |
<?php wp_nonce_field( 'post_nonce_action', 'post_nonce_field' ); ?> | |
<input type="submit" value="<?php _e( 'Enviar para revisión', 'scv' ); ?>"> | |
</form><!-- #scv-submission-form --> | |
<?php | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
return $output; | |
} | |
// Create the shortcode | |
add_shortcode( 'submission_form', 'scv_submission_form'); | |
// Create the post | |
function scv_add_pending_post(){ | |
// Validate that the contents of the form came from the location on the current site and not somewhere else | |
if( isset( $_POST['post_nonce_field'] ) | |
&& wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce_action')){ | |
global $current_user; | |
$userId = $current_user->ID; | |
// Validate & escape the form fields | |
$postTitle = $_POST['post-title']; | |
$postContent = $_POST['post-content']; | |
if( empty($postTitle) || empty($postContent) ){ | |
// Show error | |
add_action( 'scv_alerts', 'scv_form_error' ); | |
}else{ | |
$postId = wp_insert_post( array( | |
'post_content' => $postContent, | |
'post_title' => $postTitle, | |
'post_status' => 'pending', | |
'post_author' => $userId, | |
'post_type' => 'post' | |
) ); | |
// Show success | |
add_action( 'scv_alerts', 'scv_form_success' ); | |
} | |
} | |
} | |
add_action( 'init', 'scv_add_pending_post'); | |
// Form error alert | |
function scv_form_error(){ | |
echo '<p class="scv-alert error">' . __( 'Completa todos los campos del formulario', 'scv' ) . '</p>'; | |
} | |
// Form success alert | |
function scv_form_success(){ | |
echo '<p class="scv-alert success">' . __( 'Se ha publicado correctamente', 'scv' ) . '</p>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment