Blog title is not display in browser - wordpress

My permalink settings as below
/%category%/%postname%/
if i click on my blog or post it doesn't show the title in the browser page title.Please help me to fix.
enter image description here

Use this function in your function.php file
add_filter( 'wp_title', 'wpse_174379_show_posts_page_wp_title' ); function wpse_174379_show_posts_page_wp_title( $title ) { if( get_option( 'page_for_posts' ) ) { $posts_page = get_post( get_option( 'page_for_posts') );
$title = ' ' . $posts_page->post_title . ' ';
} return $title; }
I think its working for you

try this one...
<title><?php if (is_front_page() ) { bloginfo('name'); echo ' - '; bloginfo('description'); } else { wp_title(''); echo ' - '; bloginfo('name'); };?></title>

Related

Wordpress: inserting author image/avatar as og:image in head

In a post template I'm using a little code to show the author's image/avatar instead of a featured image.
<?php if ( $logo = get_avatar( get_the_author_meta( 'user_email' ), 200 ) ) : echo $logo; ?>
all well.. the author's logo is there
But now, as I finishing the project, I would like to add 'og:image' metas in the
I hoped to use a variation of this code in the function.php, but that does not work at all.
Now I have this:
add_action('wp_head', 'add_meta_tags',2);
function add_meta_tags(){
global $post;
$author_id = get_post_field('post_author' , $post->ID);
$ogimg = get_avatar_url($author_id->ID, array("size"=>400 )) ;
if( is_single() ) {
echo '<meta property="og:image" content="'. $ogimg .'" />';
}
}
$author_id gives the ID of the author, but I don't get the avatar of the author.
Couldn't figure out how to use the get_avatar_url function, but found a workaround by extracting the src value from the couple img tag like this:
add_action('wp_head', 'add_meta_tags',1);
function add_meta_tags() {
global $post;
$author_id = get_post_field('post_author' , $post->ID);
$rawimg = get_avatar($author_id, 800) ;
$doc = new DOMDocument();
$doc->loadHTML($rawimg);
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
$ogimg = $tag->getAttribute('src');
echo '<meta property="og:image" content="'.$ogimg .'" />';
echo '<meta name="twitter:image" content="'. $ogimg .'" />';
}
}

How to make a change to a file in wp-includes?

I'm using the Hestia theme. I created a child theme for Hestia.
Hestia theme
wp-content/themes/hestia
Child Theme
wp-content/themes/hestia-child
I need to make a change in the file wp-includes/general-template.php
How Can I make the change below without directly altering the file from wp-includes?
wp-content/themes/hestia/archive.php
<?php the_archive_title( '<h1 class="hestia-title">', '</h1>' ); ?>
wp-includes/general-template.php
// This function calls get_the_archive_title
function the_archive_title( $before = '', $after = '' ) {
$title = get_the_archive_title();
if ( ! empty( $title ) ) {
echo $before . $title . $after;
}
}
// Original
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
}
}
// What I need to change - 'Author: %s' to '%s'
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( '%s' ), '<span class="vcard">' .get_the_author() . '</span>' );
}
}
Some "filter" and "hook" putting this in your functions.php :
Please check below reference link.
Link1 :- https://wordpress.stackexchange.com/questions/60605/function-to-change-a-label-username-in-a-core-wordpress-file-wp-includes-gene/60607?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Link2 :- https://wordpress.stackexchange.com/questions/255677/how-to-modify-files-inside-wp-includes-directory-in-wordpress?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
add_filter( 'get_the_archive_title', function ( $title ) {
if( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
});

Escaping untrusted data in php for my wordPress theme

I have got a comment from someone superior to me who wants me to escape some comment in two php snippets which I have posted below. The problem is I don't know how to do that. Can anyone help me by modifying the snippets.
Comment I got:
Comment #1:
Validate and/or sanitize untrusted data before entering into the database. All untrusted data should be escaped before output.
Code Snippet #1:
<?php
if ( get_header_image() && !('blank' == get_header_textcolor()) ) {
echo '<div class="site-branding header-background-image" style="background-image: url(' . get_header_image() . ')">';
} else {
echo '<div class="site-branding">';
}
?>
Code Snippet #2:
<?php
printf(
/* translators: %1$s = text link: sangeet, URL: http://wordpress.org/themes/sangeet/, %2$s = text link: Kiran Kumar Dash, URL: https://twitter.com/TheKiranDash */
__( 'Theme: %1$s by %2$s', 'sangeet' ),
'' . esc_attr( 'sangeet', 'sangeet' ) . '',
'' . esc_attr__( 'Kiran Kumar Dash', 'sangeet' ) . '' );
?>
Comment #2:
esc all get_permalink() in content.php
Code snippet #3
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
My approach:
I used esc_url to esc the get_permalink() in the snippet. Shall I use esc_all? Or esc_url is just fine.
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . esc_url(get_permalink()) . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
As requested:
PS. I didnt write this, only advanced it.
Usage:
$user = sanctify($_GET['user']);
Function:
function sanctify($data){
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
$data = str_replace('"','',str_replace("'","",$data));
do{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}

Find the comment's post title

I am trying to create a plugin what will be a sime kind of a comments archive.
I have looked at get_comment().
I am looking to show where the comment came from, specifically the title of the post where the comment is attached. This is part of my code:
if ( $comments ) {
foreach ( $comments as $comment ) {
// here you can display the comment in the way you want
echo 'from: ' . $comment->i want comment post title here . '<br/>';
echo '<p>' . $comment->comment_content . '</p>';
}
}
How to do this?
Here is how you do it:
<?php
if ( $comments )
foreach($comments as $comment){
// here you can display the comment in the way you want
echo 'from: ' . get_the_title($comment->comment_post_ID) . '<br/>';
echo '<p>' . $comment->comment_content . '</p>;
}
?>

WordPress wp_title blank on index page

I'm new to WordPress and just installed version 3.3.1.
I did some googling regarding this question and found some answers but they were relevant to version 2.7 and were 2-3 years old.
Basically, the wp_title function works fine on every page except my home page where it returns blank and I get no title whatsoever. I could just hard code the title in but I'd rather not do that.
Guilty line of code:
<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>
I couldn't find anything regarding this problem happening in 3.3.1 so clearly I've done something wrong.
Here's is what I read from Codex:
If you are using a custom homepage with custom loops and stuff, you
will have an empty wp_title. Here goes a neat hack to add the
description/tagline at the wp_title place on homepage:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
So use is_front_page() to get the title on homepage, the way it is suggested in above code.
But if you use a static home page, this is the code:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
Update
for WordPress versions (>= 4.4)
Try this
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'some_name' );
Do this in functions.php and remove 'title' tag from head...
Working off of Amna's answer, I came up with the following code which should display the page title when there is one, followed by the site name.
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
Post/Page Outputs: "The Page Title - Site Name"
Home Page Outputs: "Site Name"
Obviously, this can also be swapped to display the site name first.
<?php bloginfo('name'); wp_title(' - '); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name"
This can also be combined with a conditional to display the site description when viewing the home page.
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name - The Site Description"
For google search on wordpress wp_title empty this is the first result. So I thought that I might share the most elegant solution for this.
In functions.php add a filter for wp_title.
function custom_wp_title( $title, $sep ) {
if ( is_feed() ) {
return $title;
}
global $page, $paged;
// Add the blog name
$title .= get_bloginfo( 'name', 'display' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );
The new hack from Codex is as follows:
<title><?php wp_title(''); ?></title>
Then in your "functions.php" from theme file :
add_filter( 'wp_title', 'baw_hack_wp_title_for_home' );
function baw_hack_wp_title_for_home( $title )
{
if( empty( $title ) && ( is_home() || is_front_page() ) ) {
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
return $title;
}
I use this one and it never failed:
function pageTitle($echo){
$title = "";
global $paged;
if (function_exists('is_tag') && is_tag()) {
$title .= single_tag_title(__("Tag Archive for "" , 'circle'),false);
$title .= '" - ';
}
elseif (is_archive()) {
$title .= wp_title('',true);
//$title .= __(' Archive - ' , 'circle');
$title .= __(' - ' , 'circle');
}
elseif (is_search()) {
$title .= __('Search for "' , 'circle') . esc_html(get_search_query()).'" - ';
}
elseif (!(is_404()) && (is_single()) || (is_page())) {
$title .= wp_title('',true);
$title .= ' - ';
}
elseif (is_404()) {
$title .= __('Not Found - ' , 'circle');
}
if (is_home()) {
$title .= get_bloginfo('name');
$title .= ' - ';
$title .= get_bloginfo('description');
}
else {
$title .= get_bloginfo('name');
}
if ($paged>1) {
$title .= ' - page ' . $paged;
}
if ( !$echo ) return $title;
echo $title;
}
Note that there are translation domains in it that you might want to change.
no need. Just add the <? Php wp_head ();?> Code at the end of the header.php
good luck.
I use this method in my WordPress site
//Meta Header
if ( ! function_exists( 'dima_wp_title' ) ) :
function dima_wp_title( $title ) {
if ( is_front_page() ) {
return get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
} elseif ( is_feed() ) {
return ' | RSS Feed';
} else {
return trim( $title ) . ' | ' . get_bloginfo( 'name' );
}
}
add_filter( 'wp_title', 'dima_wp_title' );
endif;
Late to the conversation...
But if you want to use the actual title of the page that you are using for the static front page, you can use the following:
if (is_front_page())
{
$title = single_post_title( '', false );
}
Although, in the actual source for wp_title(), there is the following, specificaly disabling this for the static front page:
if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
$title = single_post_title( '', false );
}
I suspect there is good reason for this. So, proceed with caution.
My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.
Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:
<title><?php wp_title( '|', true, 'right' ); ?></title>
I consulted the following page – https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/
You could also put something like this inside your title tag:
<?php
if (is_front_page()) { ?>
Home | <?php bloginfo('description');
} else {
wp_title('|', 'true', 'right'); bloginfo('description');
} ?>

Resources