Solved

Adjusting Wordpress Plugin PHP

Posted on 2011-09-10
4
303 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now