WP User Frontend plugin supports some actions and filters by itself. That means you can extend this plugin without touching it’s code. Lets see how to do that.
Adding a input field
You can add your custom input fields in 5 areas in the post add form, they are:
- Top area: wpuf_add_post_form_top
- Before the post description: wpuf_add_post_form_description
- After the post description: wpuf_add_post_form_after_description
- After the tags area: wpuf_add_post_form_tags and
- At the bottom: wpuf_add_post_form_bottom
These are the action hooks and you can bind your function to display/process anything you want. Lets create a function that will add a text field before the post description area –
/**
* Add a field to the add post area
*
* @uses `wpuf_add_post_form_description` action hook
*
* @param string $post_type the post type of the post add screen
* @param object|null $post the post object
*/
function wpufe_artist( $post_type, $post = null) {
$artist = ( $post != null ) ? get_post_meta( $post->ID, 'artists', true ) : '';
?>
<li>
<label for="song_artist">
Artist <span class="required">*</span>
</label>
<input class="requiredField" value="<?php echo esc_attr( $artist ); ?>" type="text" name="song_artist" id="song_artist" minlength="2" />
<div class="clear"></div>
</li>
<?php
}
And we can show this field by binding the function in any of those 5 action hooks mentioned before.
add_action( 'wpuf_add_post_form_description', 'wpufe_artist', 10, 2 );
These hooks can receive 2 parameters, the first one is the post_type and second one is the post object (only available on edit post area, otherwise set to NULL).
Validating the input
Now that we’ve added our field to the posting form area, we can validate that field if we want.
/**
* Validate the artist name
*
* @uses 'wpuf_add_post_validation' filter
*
* @param array $errors errors array
* @return array errors array
*/
function wpufe_artist_validation( $errors ) {
if( $_POST['song_artist'] == '' ) {
$errors[] = 'Please enter the artist name';
}
return $errors;
}
add_filter( 'wpuf_add_post_validation', 'wpufe_artist_validation' );
use the “wpuf_add_post_validation” filter for validating the input and return the errors.
Note: For a reason, the errors are hidden by default. That means, if any validation fails and return errors, you’ll not see the errors.
There is a line in “wpuf-add-post.php” like this:
//echo wpuf_error_msg( $errors );
it means the errors are not shown although if it has any. The reason behind this approach is to escape PHP’s “header already sent” message (for now).
for edit post validation, use this filter “wpuf_edit_post_validation” just like “wpuf_add_post_validation” filter
Processing the input
Now that you’ve validated the user input, it’s time to process the data.
There is a action hook wpuf_add_post_after_insert runs after the new post creation, it returns the post id. So you can grab the post ID and process as you want.
/**
* Add the artist after new post creation
*
* @uses `wpuf_add_post_after_insert` action hook
*
* @param int $post_id the newly created post id
*/
function wpufe_add_artist( $post_id ) {
update_post_meta( $post_id, 'artist', $_POST['song_artist'] );
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_add_artist' );
for edit post area, use this action hook: “wpuf_edit_post_after_update”
Other options
Modifying the new post arguments:
You can modify the post arguments before running the wp_insert_post( $args ) function. For example, if you want to change the post status forcefully -
/**
* Modify the post array
*
* @uses `wpuf_add_post_args` filter hook
*
* @param array $my_post the post array
* @return array the post array
*/
function wpufe_change_post_status( $my_post ) {
$my_post['post_status'] = 'pending';
//want to change the post author?
$my_post['post_author'] = 1;
//must return
return $my_post;
}
add_filter( 'wpuf_add_post_args', 'wpufe_change_post_status' );
for edit post area, use this filter: “wpuf_edit_post_args”
Redirecting after new post:
You can set the redirection url after the new post creation.
/**
* Change the direction url
*
* @uses `wpuf_after_post_redirect` filter hook
*
* @param string $url the redirection url
* return string new redirect url
*/
function wpufe_force_redirection( $url ) {
return home_url('/dashboard/');
}
add_filter( 'wpuf_after_post_redirect', 'wpufe_force_redirection' );
Showing info on dashboard:
There is a hook on the plugin dashboard area wpuf_dashboard. It accepts two parameter, the user id and the currently showing post_type.
/**
* Show in dashboard
*
* @uses `wpuf_dashboard` action hook
*
* @param int $user_id the current users user id
* @param string $post_type
*/
function weufe_dashboard_info( $user_id, $post_type ) {
$user = get_userdata( $user_id );
echo 'Hello ' . $user->display_name . ', Having fun?';
}
add_action( 'wpuf_dashboard', 'weufe_dashboard_info', 10, 2 );
Disabling posting capability:
You can even block a specific user from posting and show him a message/cause. Helpful for blocking spammers.
Lets check if a user has a meta field “paid” with the value “yes”, so we can let the users posting who paid for something, others will see a message.
/**
* Check if the current user can post
*
* @return bool
*/
function wpufe_can_post() {
$user = wp_get_current_user();
$paid = get_user_meta( $user->ID, 'paid', true );
if( $paid == 'yes' ) {
return true;
}
return false;
}
/**
* Block the user if hasn't the post capability
*
* @return string
*/
function wpufe_block_user( $can_post ) {
if( wpufe_can_post() ) {
return 'yes';
}
return 'nope';
}
add_filter( 'wpuf_can_post', 'wpufe_block_user' );
/**
* Show the message if can't post
*
* @param string info message
* @return string info message
*/
function wpufe_show_message( $info ) {
if( !wpufe_can_post() ) {
$info = 'Sorry, you have to pay first';
}
return $info;
}
add_filter( 'wpuf_addpost_notice', 'wpufe_show_message' );
Modifying the plugin options
You can also change the plugin options and add your own options to the plugin settings area. Lets take a look how to add “EURO” as a currency in the plugin options area.
/**
* Adds new currency to admin options
*
* @param array $fields admin options
*/
function wpufe_currency_filter( $fields ) {
foreach( $fields as $key => $val ) {
if( $val['name'] == 'wpuf_sub_currency' ) {
$fields[$key]['options']['EUR'] = 'EURO';
}
}
return $fields;
}
add_filter( 'wpuf_build_form_args', 'wpufe_currency_filter' );
The plugin settings are build from a big array, so you can add/modify the array. So handy!