I am creating a plugin that creates custom fields on the author profile page.
-It creates custom fields on the user profile page -We can save it for every user.
What is happening now: -Creates field and it can be seen in the front end of the author profile page -On saving the fields do not save.
Observation: I saw the custom meta key and data is not there is the user meta table. So I am thinking we are able to see fields in the front end but database meta key values are not created.
The same code works perfectly when added to functions.php. Please let me if I am missing anything or if any other hooks need to be fired when adding as a plugin.
<?php
class authorFieldsPlugin {
function __construct() {
//Uses author.php from plugin of not there in theme.
add_filter( 'template_include', 'wpa_155871_template_loader' );
function wpa_155871_template_loader( $template ) {
$file = '';
if ( is_author() ) {
$file = 'author.php'; // the name of your custom template
$find[] = $file;
$find[] = 'plugin-name/' . $file; // name of folder it could be in, in user's theme
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
if ( ! $template ) {
// if not found in theme, will use your plugin version
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/' . $file;
}
}
return $template;
}
function memberpage_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','memberpage_rewrite');
//Custom profile fields
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Author Information", "blank"); ?></h3>
<table >
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span ><?php _e("Please enter Author's Information from plugin."); ?></span>
</td>
</tr>
<tr>
<th><label for="author_title"><?php _e("Author Title"); ?></label></th>
<td>
<textarea name="author_title" id="author_title" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author_title', $user->ID ) ); ?></textarea><br />
<span ><?php _e("Please enter author_title."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'author', $_POST['author'] );
}
}
}
$authorFieldsPlugin = new authorFieldsPlugin(); ?>
CodePudding user response:
What I can spot is that you're only saving one field, 'author', but you're creating two fields, 'author' and 'author_title' you should also save the second field in the same way you saving the first one. dd the following code to save the 'author_title' field after the 'author' field;
update_user_meta( $user_id, 'author_title', $_POST['author_title'] );
You should check if the $_POST variables are set or not before trying to update the user meta,
if( isset( $_POST['author'] ) ) {
update_user_meta( $user_id, 'author', $_POST['author'] );
}
if( isset( $_POST['author_title'] ) ) {
update_user_meta( $user_id, 'author_title', $_POST['author_title'] );
}