Wordpress - Create link to separate author archive inside blog post - php

We're developing a blog in Wordpress and have come across some functionality that we're having some difficulty implementing.
Our client wants to be able to mention blog users in a blog post, this will then take the end user to the mentioned authors profile. Example below:
"We recently spoke to our newest team member Pete Smith" or "We recently spoke to our newest team member #PeteSmith"
If you were to click on Pete's name it would take you to website.com/authors/petesmith
I know that Wordpress has mentioning functionality built into the comments section, but is there a way to achieve this inside actual blog posts?
Apologies that I can't include any code on this question, there simply is nothing to show you guys.

You can use something like this inside your functions.php:
add_filter('the_content', 'the_content_func');
function the_content_func($content) {
$ret = preg_replace_callback ( "/(#[A-Za-z]+)/",
function ($match) {
$authorName = mb_substr($match[0], 1);
$author = get_user_by('slug', $authorName);
if ( ! empty( $author ) )
return '' . $author->display_name . '';
else
return $authorName;
},
$content);
return $ret;
}
I assume that the text after # symbol is the author's slug, so inside the filter I search for the given author and if he's found, output the corresponding author profile link with the display name. Otherwise I just output the string without # symbol & URL.
If I misunderstood your goals, feel free to modify the inner function of preg_replace_callback keeping in mid that $match[0] contains found user slug with # symbol from the post content.

Here is code for author archive page
<?php the_author(); ?>

Related

Matching user meta fields with custom post meta fields

I am trying to create a function that matches user's that have the same expertise as a custom post custom field. So if a custom author meta has an expertise of 'Ninja' and the custom post type has a custom field that also has 'Ninja', my email will go out to all those matching user's.
I have the following bit of wp_mail code and can also create user queries using get_users but cannot get the two to work as i need. Any ideas?
add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish', 'send_emails_on_new_event');
add_action('auto-draft_to_publish', 'send_emails_on_new_event');
function send_emails_on_new_event($post) {
global $post;
$expertise = get_field('expertise', $postid);
$emails = MATCHING USER EMAIL ADDRESSES TO GO HERE;
$message = '<p>Email content will go here ...</p>';
if (get_post_type($post->ID) === 'custom_post_type') {
wp_mail($emails, "Email title will go here ...", $message);
}
}
Have you tried using SQL query actually to get matching emails from database??
something like
$sql = SELECT user.email FROM user WHERE user.expertise = $postExpertise;
$email = database->getRecords($sql); //getRecords() is just general function placeholder, build your own db processor class
...
function send_emails_on_new_event($post) {
...
}
You have to understand, WP is just a tool written in PHP with common functionality, if you want custom functionality, either build it yourself or if you are lucky, find plugin already created with similiar functionality. But you will not always find the plugin you want.
Hence why WP is good only for low-budget websites built by not so great web developers. Because if you can build custom functionality on top of WP, you should already consider if using WP is even good idea in first place

PHP Multisite Wordpress, need 2 functions to work without breaking website

I'm completely new and not a programmer, I'm just experimenting with the functions.php file (in a child theme) to see if I can get a function to work.
My goal: I am using a multisite wordpress setup for a website on models. When admin creates a profile for each model, their name is used on SITE 1 for their unique url AND SITE 2 to say 'welcome model name'
SITE 1: public site for everyone to see
SITE 2: private models only site to view and update personal details
On SITE 1, I have a complete list of all the models on a page for the public to browse through. Each model has their own unique profile URL that contains their first and last name. E.g. http://www.domain.com/models/samantha-rebecca
On SITE 2, when the models log in to the site, I need the homepage to show 2 name uses. One is their display name using a shortcode so I can say 'Welcome Samantha Rebecca' (which I have got working using this code).
/* username in page content using [current_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->display_name;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');
The other is for a button on the same private page they can press to go straight to their 'public' profile page. This is what I need help with please.
In an ideal world, I would like to take their first and last name (all profiles are admin created only) and put a dash in between so I can use http://www.domain.com/models/[url_user] for which I did try and use the following:
/* username in url using [url_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_firstname . '-' . $current_user->user_lastname;
$output = ob_get_clean();
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func');
But I can't seem to get them to both work at the same time in the functions file without an error coming up such as this
Fatal error: Cannot redeclare custom_shortcode_func() (previously declared in /home/domain/public_html/wp-content/themes/child-theme/functions.php:47) in /home/domain/public_html/wp-content/themes/child-theme/functions.php on line 65
Please can someone help me get this to work as one efficient function. Thank you.
i got your issue is in both short code you have used same function name please change your second function name like below code.
/* username in url using [url_user] shortcode */
function custom_shortcode_func_url_username() {
ob_start();
$current_user = wp_get_current_user();
$output = $current_user->user_firstname . '-' . $current_user->user_lastname;
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func_url_username');
change your second function name like above code your problem will solved. you are getting error because for both shortcode your function name is same. function name should be unique. same name multiple time is not allowed.
more about used define function name rules. More about used define functions

How to filter posts by author on edit.php

Let's suppose the user visits the page of
http://mywpsite/wp-admin/edit.php?author=john-doe
Let's suppose there is an author with the name of 'John Doe' and the given author has three posts. Yet, when I visit the page, I see an empty grid, as if there were no posts created by this author.
I would like to search for posts created by the given user. Based on my research, I can see that people are claiming that something like this should work:
function posts_for_current_author($query) {
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Source.
However, here the author's ID is expected as input, yet, I do not know how to get the author's id by the slug of 'john-doe'.
How can I get the ID of the author by slug and search for posts based on that ID?
EDIT:
This is one failed try, based on NATH's comment:
function wpshock_search_filter( $query ) {
if ((is_admin()) && (isset($_GET["author"])) && (preg_match('/[^a-zA-Z_0-9]/i', $_GET["author"]))) {
$query->set("author_name", $_GET["author"]);
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
The query still returns no elements. I have var_dumped $query and seen that $query->query["author"] has the textual problem, which is a potential problem. Also, $query->tax_query contains data related to author. I am sorry if this question is worthy of down-votes, I thought others might be confused by Wordpress's database handling as well and thus this question might be useful. Maybe I was wrong.
This should work for you :
get_user_by('slug','john-doe');
EDIT :
From this you can get author object and you can carry forward with your code.
Link
WordPress provides an author_name query variable to you. You can use the following by default:
http://mywpsite/wp-admin/edit.php?author_name=john-doe
Read more about public query variables in the Codex.
I'm wotking on wp front-end and reach similar feauture you are requested
The code you provide are woking current on wp-admin page but any one uses that code prevent his own SuperAdmin account form reviewing other editors posts .
just add !is_super_admin() to your first contition like this :-
if($query->is_admin && !is_super_admin() )
BTW the author's ID are already provided with global $user_ID;
and when you add this $query->set('author',$user_ID); the main query will filter posts result accurding to author ID , see WP_Query();
See this-
https://wordpress.stackexchange.com/questions/89990/how-we-can-get-the-author-id-by-its-name
And, a better way to get posts by an author would be-
get_posts('author'=>AUTHOR_ID);

Wordpress: Inserting Category headers into Page TITLE

To provide better information to potential visitors to my site, I'd like to have the Categories listed in the page TITLE.
For example:
Dead bankers list 2015
would appear as
Dead bankers list 2015 | conspiracy, current affairs, higher form of life, insane, new world order, us bailout
Currently I'm able to use just one category header, but I'm aware people may be searching under any number of these to turn up my resource as a valid response.
You can define new title as per your requirement in template single.php
or you can add a filter as below
add_filter('the_title','some_callback');
function some_callback($data){
global $post;
// where $data would be string(#) "current title"
// Example:
// (you would want to change $post->ID to however you are getting the book order #,
// but you can see how it works this way with global $post;)
return 'Book Order # | ' . implode(',' , get_the_category( $post_id ));
}

Why does WordPress add a duplicate numeric slug upon post/page view?

This is my first post here, so I apologize in advance for any mishaps.
I've been searching for hours trying to figure this one out, but I simply can't seem to understand why this is happening.
The site I'm setting up is a child site (not in a multisite sense, but as a separate site/domain with the same branding). Some of the posts on my site will originate from the parent/main site (but will be made as new posts through copy-paste), and I want the original article ID as part of the permalinks.
E.g. http://www.example.com/hello-world/12345/, where 12345 is the article ID of the article on the parent/main site.
To accomplish this, I've added a custom field to my posts where I can add the article ID of the original article with external_article_id as Field Name. I've then tried to manipulate the permalinks with the following code:
add_filter('post_link', 'append_custom_permalink', 10, 2);
function append_custom_permalink($url, $post) {
$newurl = $url;
if ($post->post_type == 'post') {
$custom = get_post_custom_values('external_article_id', $post->ID);
if (!empty($custom))
$newurl = $url . $custom[0] . '/';
}
return $newurl;
}
Whenever I output the permalink to the posts it appears exactly as I want it, both in the editor and on the site. However, when I either click a link or enter the address manually, I get redirected automatically to http://www.example.com/hello-world/12345/12345/. It duplicates the additional numerical slug, and also happens when I replace $custom[0] with a hard-coded numeric value. This applies to all posts, and my permalink structure (in the settings) is set to /%postname%/.
I even tried setting the permalink structure to /%postname%/%ext_article_id%/ and replace %ext_article_id% with $custom[0], but with the exact same outcome. I also tried using the same code on another WordPress site, except this time with pages instead of posts, also with the exact same outcome.
Ideally I would like to use something like add_query_arg($custom[0], '', get_permalink($post->ID));, but omit the question mark that comes along with it.
Could someone please explain to me why this is happening, and how I can circumvent this? Do I need to use some other filter, or how can I approach this?
Thank you in advance!
In order to make this work you also need to make WordPress aware of the rewrite_tag and specify an additional permalink structure via add_permastruct. The following code should do the trick:
function append_custom_permalink( $post_link, $post ) {
if( $post->post_type == 'post' ) {
$custom = get_post_custom_values( 'external_article_id', $post->ID );
if( ! empty($custom) ) {
$post_link = $post_link.$custom[0].'/';
}
}
return $post_link;
}
add_filter('post_link', 'append_custom_permalink', 10, 2);
function sof_30058470_posts_rewrite() {
add_rewrite_tag('%external_article_id%', '([^/]+)', 'external_article_id=');
add_permastruct('external_article_id', '/%year%/%monthnum%/%day%/%postname%/%external_article_id%/', false);
}
add_action('init', 'sof_30058470_posts_rewrite', 10, 0);
Make sure to re-save your permalink structure at Settings->Permalinks once you added the code. You may also need to refresh/clear your browser cache.

Categories