Show content if the post belongs to a specific author - wordpress

I would like to know if there is any way to enable a script for visitors if the post they visit belongs to a specific author.
It is a script that I want to include in header.php or in single.php, if the visitor goes to a post that belongs to James, show or enable the following script in the header:
<script src="/wpauthors/james.js"></script>

function authScript() {
if(get_the_author_meta('ID' == 2) { // 2 is James uer ID here
echo '<script src="/wpauthors/james.js"></script>';
}
}
add_action('wp_head', 'authScript');
Add this to your functions.php. It should work. Haven't tested this myself.

Related

how to get the last read post name by logged user in wordpress?

I need the get the name of the last post read by logged user in Wordpress. To use it as a hint on the logged user dashboard. "Your last read post was {post_name}"
Could you please advise how can i get this last read post ID or name?
Thank you.
Add following code in your theme's functions.php or plugin file.
add_action( 'wp', 'wp_user_last_view_post_func' );
function wp_user_last_view_post_func(){
if (is_user_logged_in() AND is_singular() AND get_post_type()==="post"){
update_user_meta(get_current_user_id(),'last_view_post_id',get_the_ID());
}
}
add_action('wp_dashboard_setup', 'last_post_dashboard_widgets');
function last_post_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('last_read_post_widget', 'Last Read Post', 'last_read_post_widget_dashboard');
}
function last_read_post_widget_dashboard() {
$last_post_id = get_user_meta(get_current_user_id(),'last_view_post_id',true);
echo '<p>Your last read post was '.get_the_title($last_post_id).'</p>';
}
You can see last read post on wordpress dashboard like this : See Attachment
I hope this may be helpful to you.
Thanks

How to display content based on post author?

I know that Jetpack's 'widget visibility' option can be used to display widgets on specific author pages. I would like to be able to do this in my theme's template file.
The list of conditional tags has options to see if one is on a specific author's archive page but not, seemingly, to check who the specific author of a post is.
This is basically what I want to do:
If author is Mr. Incredible, show some html. If author is Mrs.
Incredible, show different html.
Is this possible? I imagine it should be but I haven't been able to find any how to on it yet.
Sure! Your problem statement is a great start. Let's translate it into actual code:
If author is Mr. Incredible, show some html. If author is Mrs. Incredible, show different html.
Becomes:
if ( is_author('Mr. Incredible Nickname or ID') ) {
// echo some HTML
} elseif ( is_author('Mrs. Incredible Nickname or ID') ) {
// echo different HTML
} else {
// if neither, echo something else
}
The code should be readable, just like the problem statement.
You can read more about is_author() in the Codex.
Additionally, if you need to check the author of individual posts, you can use get_the_author().
You can get post author ID.
With use it, get author nickname or etc...
And check your want with this.
Tip for you:
in single.php
<?php
$ID = $post->post_author;
$nickname = get_the_author_meta('nickname', $ID);
if($nickname == "fors")
include(TEMPLATEPATH . '/fors-template.php');
else
include(TEMPLATEPATH . '/single-default.php');
?>

How to add page URL to list of pages in WordPress admin panel?

Currently, by default, the following columns appear in the list of pages in the WordPress admin panel:
Title
Author
Comments
Date
and because I have AIO SEO installed:
SEO Title
SEO Description
SEO Keywords
Is there a way to have WordPress also display the URL to the page (at least the part of the URL that is created when the page itself is created)?
The page url is actually already there by default, it's just hiding. When you hover over a page title, several links appear below the title -- edit, quick edit, trash, view. View is the hyperlink to the page, which you can click to view the page, or right click and copy the link address to use elsewhere.
Otherwise, if you are using a custom/child theme, you could add the following to your functions.php file:
add_filter('manage_page_posts_columns', 'my_custom_column', 10);
add_action('manage_page_posts_custom_column', 'add_my_custom_column', 10, 2);
function my_custom_column($defaults) {
$defaults['url'] = 'URL';
return $defaults;
}
function add_my_custom_column($column_name, $post_id) {
if ($column_name == 'url') {
echo get_permalink( $post_id );
}
}
Note: This just creates a text url to your page.
Also note, you do not want to edit your functions.php file directly if you are using a theme you did not create, as it will be overwritten when you update. If you want to add this to an existing theme, I'd suggest looking into child themes.
This is helpful. I would only improve the output slightly by removing the site url and just showing the page. Takes up less space and less to weed through visually.
if ($column_name == 'url') {
$siteURL=get_site_url($post_id);
$link= get_permalink( $post_id );
echo str_replace($siteURL,"",$link);
}

Wordpress Redirect page

I search several times but I couldn't find nothing that really helps me.
Basically I have a very VERY simple Theme which is working very well.
I also have a database with some IP's and depending by this IP I need to redirect the guest to different pages.
So on my template index.php file I added:
if ($country == "br") {
echo "case1";
} else {
$location = bloginfo('template_url')."/index-nbr.php";
echo $location;
?>
<script type="text/javascript">
window.location= <?php echo "'" . $location . "'"; ?>;
</script>
<?php
}
I tried many different ways to redirect but none of them work. Could someone help me with this stuff?
Ok let me make your life easier.
Just install this plugin you will find a text box on the bottom of every page in PAGES section in Admin panel where you can put your desired link/URL of the page and that will redirect you to your inserted URL.
Page Link to Plugin

Make Wordpress Pages's Titles readonly

I'm looking for a WP function that add the Read-only parameter to all Pages's Titles's input, that will make the Page's title unalterable.
Thanks a lot in advance.
This can be accomplished with some simple JavaScript/jQuery. Create a file called admin_title_disable.js, and queue it up within functions.php. For example:
functions.php:
wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
function disableAdminTitle () {
wp_enqueue_script('admin_title_disable');
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');
Now, in your js file:
jQuery(document).ready(function ($) {
$('#title').attr('disabled','disabled');
});
This will set both post and page title input fields with a disabled attribute. Hope this helps!
If you want to restrict this script to a particular admin page, wrap the add_action hook in a conditional that compares $_GET['page']. You can also take advantage of the $hook parameter that is available when using admin_enqueue_scripts to check for the page. See here.
Update::
WordPress makes it a little tricky to tell between post and page edit screens, but there is a hidden input that you can take advantage of. :) Here's an updated version of the jQuery that will only run on page edit screens:
jQuery(document).ready(function ($) {
//find the hidden post type input, and grab the value
if($('#post_type').val() === 'page'){
$('#title').attr('disabled','disabled');
}
});
No need to make a seperate js file. Adding this to your function.php will do the same that Matthew showed.
function admin_footer_hook(){
?>
<script type="text/javascript">
if(jQuery('#post_type').val() === 'post'){
jQuery('#title').prop('disabled', true);
}
</script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );
This Solution Will disable clicking on the post title and editing it using CSS. CSS targets post type "page" only. It has been tested on Gutenberg visual editor. Users Can still edit title from "Quick Edit".
Add this code to your functions.php file.
function disable_title_edit() {
if(!current_user_can('administrator')){
if( !current_user_can('administrator')){ ////Only allow Admin
echo '<style>.post-type-page .edit-post-visual-editor__post-title-wrapper{
pointer-events: none;
}</style>'; } }
}
add_action('admin_head', 'disable_title_edit', 100);

Resources