I know how to change the base of the author permalinks, however on my site, I refer to users not by their username but by a number based on their User ID, so User number 5 wrote this post, rather than JohnDoe123 wrote this post.
The problem comes when I go to that users archives and instead of seeing something like example.com/authors/5/ I see example.com/authors/johndoe123/ .
How do I change permalinks so that I can pull up author archives using the following structure? :
[wordpress_site_url]/authors/[user_ID]/
This can be done by adding new rewrite rules for each user in exactly the same way you would when changing or removing the author base. So, adapting code from a previous answer, you would add your rewrite rules something like this:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename from {$wpdb->users}");
foreach ($authors as $author) {
$author_rewrite["authors/{$author->ID}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]';
$author_rewrite["authors/{$author->ID}/?$"] = "index.php?author_name={$author->nicename}";
}
return $author_rewrite;
}
And then filter the author link:
add_filter('author_link', 'my_author_url_with_id', 1000, 2);
function my_author_url_with_id($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = "authors/$author_id";
return $link_base . $link;
}
Actually I don't think you need to create rules for each user in this case, the following two rules should suffice:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
$author_rewrite = array();
$author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]';
$author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]';
return $author_rewrite;
}
Related
I am facing a weird problem. I am trying to override value in a function using apply_filter.
Here is the code which I am using in the theme's functions.php file:
function customized_params_6e9668( $attrs ) {
$subfolder = isset($_GET["sub"]) ? '/'.$_GET["sub"] : '';
$attrs["folder"] = $attrs["folder"].$subfolder; // if getting the value from the url then not working in this case
//$attrs["folder"] = $attrs["folder"]."/testing"; // if I use static name then working in this case
echo $attrs["folder"];
return $attrs;
}
add_filter( "customized_params_6e9668", "customized_params_6e9668");
Here is the function where I am using apply_filter to override the values in the plugin's file.
function getFolderData(){
global $wpdb;
$folder_data = $wpdb->get_row(
$wpdb->prepare(
'SELECT * FROM ' . $wpdb->prefix . 'folders WHERE key=%s',
trim(sanitize_text_field($_REQUEST["data_key"]))
)
);
if(!empty($folder_data)){
$folder_data = apply_filters( 'customized_params_'.$folder_data->key, $folder_data );
print_r($folder_data);
}
}
This function is getting the list of data from the database. overriding the value of the folder using add_filter.
Please correct me where I am doing wrong.
Thanks in advance.
I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.
Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Please change last line of your code to this:
$raised_by=ucwords($user->roles[0]);
So that your current code which display First Name i.e.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
Above code will become:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=ucwords($user->roles[0]);
}
Update: To remove underscore with space your code may become as:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
}
You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.
Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.
Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.
You could use this line of code.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = implode(', ', $user_info->roles);
}
Or, if you prefer to use the get_user_role function that you've written,
slightly modify it to take the user ID as input and return the user role.
function get_user_role($user_id) {
$user_info = get_userdata($user_id);
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You could use it like as shown below to output the user role.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = get_user_role($user->ID);
}
I want to implement custom author urls.
Currently the author urls are like this:
http://site.com/author/author-name/
I want to do something like
http://site.com/my-custom-url-here
For each individual user.
I have tried using author_rewrite_rules filter, using the following code, this converts the url correctly but this gives me a page not found message when i browse the url
add_filter('author_link', 'no_author_base', 1000, 3);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
foreach($authors as $author) {
$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
$author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
}
return $author_rewrite;
}
Any help would be greatly appreciated!
Thanks.
UPDATE
Problem is solved!, basically i did not know about calling the flush_rewrite_rules function.
Solved!
Here's how i did it:
i did not need the author_link filter so i removed it, my custom urls are stored in usermeta table so i fetched them and passed them into the rewrite array, and most importantly I DID NOT KNOW ABOUT FLUSHING THE REWRITE CACHE AND THAT WAS THE REASON MY ORIGINAL CODE WAS NOT WORKING
Heres the full code:
add_filter('author_rewrite_rules', 'my_author_url_with_custom_url_rewrite_rules');
function my_author_url_with_custom_url_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename, meta_value as profile_name
from $wpdb->users
LEFT JOIN wp_usermeta ON wp_usermeta.user_id = $wpdb->users.ID
WHERE meta_key = 'profile_name'");
foreach ($authors as $author) {
$author_rewrite["{$author->profile_name}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]';
$author_rewrite["{$author->profile_name}/?$"] = "index.php?author_name={$author->nicename}";
}
return $author_rewrite;
}
flush_rewrite_rules(false);
dont forget to comment the flush_rewrite_rules call after you are done with rewrite rules, it is very expensive!
try this one. for get your author url.
<?php the_author_posts_link(); ?>
if post a admin or a author and want to show his/her all post in one page. so, you need to get his/her url automatic. then try this one
i am currently using contact form 7 for wordpress. i would like a unique id for every form filled in and sent.
i have had a look around the internet but cant find anything, although i did find the following:
http://contactform7.com/special-mail-tags/
would there be any easy way to make my own function to do something similar to the above tags? i would need it to be a function to go into my themes function file, so that plugin updates wont affect it.
Cheers Dan
[_serial_number] field is an auto-incremented form submission ID. You just have to have Flamingo plugin installed as well. See http://contactform7.com/special-mail-tags/
(It's the same link as in the question, I guess the field wasn't there when the question was asked).
To generate ID for the Contactform7 you need to hook the 'wpcf7_posted_data'.
However, to generate incremental ID you need to save the forms in database so you can retrieve which ID should be next on next Form submit. For this you will need CFDB plugin (https://cfdbplugin.com/).
If you dont want to put the code inside theme functions.php file, you can use this plugin instead: https://wordpress.org/plugins/add-actions-and-filters/
Example code:
function pk_generate_ID($formName, $fieldName) {
//Retrieve highest ID from all records for given form stored by CFDB, increment by 1 and return.
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$start = '001';
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName>=$start&&$fieldName<999999999999"; //is_numeric() is not permitted by default for CFDB filter
$atts['limit'] = '1';
$atts['orderby'] = "$fieldName DESC";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = $start;
while ($row = $exp->nextRow()) {
$row2 = $row[$fieldName];
$row2 += 1;
$found = max($found,$row2);
}
return $found;
}
function pk_modify_data( $posted_data){
$formName = 'Form1'; // change this to your form's name
$fieldName = 'ID-1'; // change this to your field ID name
//Get title of the form from private property.
$cf_sub = (array) WPCF7_Submission::get_instance();
$cf = (array) $cf_sub["\0WPCF7_Submission\0contact_form"];
$title = (string) $cf["\0WPCF7_ContactForm\0title"];
if ( $posted_data && $title == $formName) ) { //formName match
$posted_data[$fieldName] = pk_generate_ID($formName, $fieldName);
}
return $posted_data;
}
add_filter('wpcf7_posted_data', 'pk_modify_data');
I have a plugin installed on my wordpress blog that takes the email address and message and creates a new user on my other CMS platform. Problem is this effects all comment boxes. I want to re-work this plugin to check if its on a particular page if so some values would be different. If not then proceed like usual.
function ifcj_createUser_SFI($comment_id) {
# determine commenters email
global $wpdb;
$sql = "SELECT comment_author_email FROM $wpdb->comments WHERE comment_ID = $comment_id";
$userEmail = $wpdb->get_var($sql);
if ( $userEmail == NULL ) {
return false;
}
# pull in class file
require( ABSPATH . 'class.convio_api.php');
# create instance and connection with convio, ALWAYS REQUIRED
$c = new ConvioAPI('site_id=xxx&api_key=xxxxxx&login_name=xxx#xxx.org&login_password=xxxxx');
# add user to groups/interests (this can be done when creating the user also)
$c->createUpdateUser('primary_email='.$userEmail.'&add_group_ids=73663&add_interest_ids=3761');
return true;
}
add_action('comment_post','ifcj_createUser_SFI',0,1);
Thanks in advance.
Use conditional tags: http://codex.wordpress.org/Conditional_Tags
Here is how you would test for a certain page: http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
Like:
if ( is_page( '2' ) ) {
return false;
}