Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adjusting Wordpress Plugin PHP

Posted on 2011-09-10
4
Medium Priority
?
318 Views
Last Modified: 2012-05-12
I would like to use the 'edit author slug' plugin from:

http://wordpress.org/extend/plugins/edit-author-slug/

As is, it only allows admins to edit the slug, but I want my contributors and authors to edit the slug as well.

The author of the plug in says this can be done easily... "I've included a file in the 'edit-author-slug/includes' folder named 'caps.php.' This file will give you a jumping off point to give other users the ability to edit their own slugs."

Well, perhaps it's easy, but not for me. The code in the file is posted below - I don't know how to adjust it to provide capabilities to contributors and authors.

Do you know how and can you show me?

Thanks,                Chris



<?php

/**
 * Edit Author Slug Capabilities
 *
 * @package Edit Author Slug
 * @subpackage Capabilities
 *
 * @author Brandon Allen
 */

/**
 * I originally started this file to add a custom capability for
 * later use within the plugin. During development I decided it
 * wasn't safe to rely, solely, on a custom cap. However, it
 * would be beneficial to others looking, for example, to allow
 * subscribers to edit their own slugs. So rather than remove
 * all traces of what almost happened, I've left this file to
 * give others a starting point.
 */

/**
 * Adds capabilities to WordPress user roles.
 *
 * This is called on plugin activation.
 *
 * @since 0.7.0
 *
 * @uses get_role() To get the administrator role
 * @uses WP_Role::add_cap() To add various capabilities
 * @uses do_action() Calls 'ba_eas_add_caps'
 */
function ba_eas_add_caps() {
	// Add cap to admin role
	if ( $admin =& get_role( 'administrator' ) )
		$admin->add_cap( 'edit_author_slug' );

	do_action( 'ba_eas_add_caps' );
}
add_action( 'ba_eas_activation', 'ba_eas_add_caps' );

/**
 * Removes capabilities from WordPress user roles.
 *
 * This is called on plugin deactivation.
 *
 * @since 0.7.0
 *
 * @uses get_role() To get the administrator role
 * @uses WP_Role::remove_cap() To remove various capabilities
 * @uses do_action() Calls 'ba_eas_remove_caps'
 */
function ba_eas_remove_caps() {
	// Add cap to admin role
	if ( $admin =& get_role( 'administrator' ) )
		$admin->remove_cap( 'edit_author_slug' );

	do_action( 'ba_eas_remove_caps' );
}
add_action( 'ba_eas_deactivation', 'ba_eas_remove_caps' );

?>

Open in new window

0
Comment
Question by:St_Aug_Beach_Bum
  • 3
4 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36517001
Something like this should work for you, I've just changed the role to is_user_logged_in:

<?php
function ba_eas_add_caps() {
        if ( $is_user_logged_in =& get_role( 'contributor' ) )
                $is_user_logged_in( 'edit_author_slug' );
        do_action( 'ba_eas_add_caps' );
}
add_action( 'ba_eas_activation', 'ba_eas_add_caps' );

function ba_eas_remove_caps() {
        if ( $$is_user_logged_in =& get_role( 'contributor' ) )
                $$is_user_logged_in->remove_cap( 'edit_author_slug' );
        do_action( 'ba_eas_remove_caps' );
}
add_action( 'ba_eas_deactivation', 'ba_eas_remove_caps' );
?>

Open in new window

0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 2000 total points
ID: 36517055
OK I've installed your plug-in, and there are 2 files that need changed.

The first is the one I posted above. I messed up and placed two $$ in two spots, so here is the code for it:

<?php
function ba_eas_add_caps() {
        if ( $is_user_logged_in =& get_role( 'contributor' ) )
                $is_user_logged_in( 'edit_author_slug' );
        do_action( 'ba_eas_add_caps' );
}
add_action( 'ba_eas_activation', 'ba_eas_add_caps' );
function ba_eas_remove_caps() {
        if ( $is_user_logged_in =& get_role( 'contributor' ) )
                $is_user_logged_in->remove_cap( 'edit_author_slug' );
        do_action( 'ba_eas_remove_caps' );
}
add_action( 'ba_eas_deactivation', 'ba_eas_remove_caps' );
?>

Open in new window


The other file that needs edited is the plug-in's functions.php. It had additional user capabilities added in the form of:
if ( current_user_can( 'edit_users' )

Open in new window


I change that code to ('contributor'), so the final code for the functions.php is:
<?php
function ba_eas_show_user_nicename( $user ) {
	if ( current_user_can( 'contributor' ) || ( current_user_can( 'edit_author_slug' ) && IS_PROFILE_PAGE ) ) : ?>
	<h3><?php esc_html_e( 'Edit Author Slug', 'edit-author-slug' ); ?></h3>
	<table class="form-table">
		<tbody><tr>
			<th><label for="ba-edit-author-slug"><?php esc_html_e( 'Author Slug', 'edit-author-slug' ); ?></label></th>
			<td>
				<input type="text" name="ba-edit-author-slug" id="ba-edit-author-slug" value="<?php echo esc_attr( $user->user_nicename ); ?>" class="regular-text" /><br />
				<span class="description"><?php esc_html_e( "ie. - 'user-name', 'firstname-lastname', or 'master-ninja'", 'edit-author-slug' ); ?></span>
			</td>
		</tr></tbody>
	</table>
	<?php endif;
}
function ba_eas_update_user_nicename( $user_id = 0 ) {
	global $wpdb;
	$user_id = (int) $user_id;
	check_admin_referer( 'update-user_' . $user_id );
	$userdata	 = get_userdata( $user_id );
	$author_slug = isset( $_POST['ba-edit-author-slug'] ) ? sanitize_title( trim( $_POST['ba-edit-author-slug'] ) ) : '';
	if ( ! empty( $author_slug ) && ( $userdata->user_nicename != $author_slug ) ) {
		$new_userdata = array(
			'ID'            => $user_id,
			'user_nicename' => $author_slug
		);
		$updated_user_id = wp_update_user( $new_userdata );
		if ( $updated_user_id )
			wp_cache_delete( $userdata->user_nicename, 'userslugs' );
	}
}
function ba_eas_add_author_base_settings_field() {
	add_settings_field( 'ba-eas-author-base', __( 'Author Base', 'edit-author-slug' ), 'ba_eas_author_base_settings_html', 'permalink', 'optional' );
}
function ba_eas_author_base_settings_html() {
	global $ba_eas;
	echo '<input id="ba-eas-author-base" name="ba-eas-author-base" type="text" value="' . esc_attr( $ba_eas->author_base ) . '" class="regular-text code" />';
}
function ba_eas_author_slug_column( $defaults ) {
	$defaults['ba-eas-author-slug'] = esc_html__( 'Author Slug', 'edit-author-slug' );
	return $defaults;
}
function ba_eas_author_slug_custom_column( $default, $column_name, $user_id ) {
	$user_id = (int) $user_id;
	if ( $column_name == 'ba-eas-author-slug') {
		$userdata = get_userdata( $user_id );
		return esc_attr( $userdata->user_nicename );
	}
	return $default;
}
function ba_eas_cleanup_options() {
	$options = get_option( 'ba_edit_author_slug', array() );
	if ( array_key_exists( 'dont_forget_to_flush', $options ) ) {
		unset( $options['dont_forget_to_flush'] );
		update_option( 'ba_edit_author_slug', $options );
	}
}
function ba_eas_courtesy_flush() {
	flush_rewrite_rules( false );
}
?>

Open in new window

0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 36517149
Wow - that is awesome (and works!).

Thank you very much. It was a 'must-have' ability before our site launch, which will hopefully be tomorrow if I can get everything wrapped up. We did not have 'author' pages before, and many users chose previous usernames that were different than their public author names - so having it as the default 'username' based url just wasn't going to work at all.  And... I did not want to go in and change them all myself :) :)

Thanks again, that was a lot of work and I appreciate it!
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36517173
You're welcome, I'm glad it worked for you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question