Page title before site name on Wordpress - wordpress

I have a WP site and am trying to figure out how to place the name of the page before the site name. Essentially, I'd like to change "ABC Inc. | About Us" to "About Us | ABC Inc.". Surprisingly, I ended up empty-handed after googling. Is this addressed by some plug-in?

You can do this in your themes header.php file by replacing your tag with the following:
<title><?php wp_title('|'); bloginfo('name'); ?></title>
http://codex.wordpress.org/Function_Reference/wp_title

It is common to see usage of
<title><?php wp_title('|'); bloginfo('name'); ?></title>
or similar uses on the <title> tags; but according to the WordPRess Codex, it should not be used:
The wp_title() function should not be used by a theme in conjunction
with other strings or functions (like concatenating with
bloginfo('name')) to write the content of the element, because
it will render plugins unable to rewrite the whole title in case the
plugins use the wp_title filter do the rewrite, which is the best
practice. The use of this function is now a requirement for theme
developers.
The best way is to include a filter in your functions.php file:
function filter_wp_title( $title ) {
$site_description = get_bloginfo( 'description' );
$filtered_title = $title . get_bloginfo( 'name' );
$filtered_title .= ( ! empty( $site_description ) && ( is_home() || is_front_page() ) ) ? ' | ' . $site_description: '';
return $filtered_title;
}
For more info on filtering the title, see the codex.

If you want to use a plugin, check out WordPress SEO by Yoast. Highly recommend it. You can drill down to writing custom titles for posts, categories, tags, homepage, etc.
Note: if it appears not to be working with your theme, click on "force rewrite titles".

Related

Trying to add an ACF field under the post title in Wordpress

My client is using the Divi builder theme and has asked me to insert a subhead under the post title on category archive pages. The theme uses the_content() to pull all content into the pages so simply adding a line under the title in the page templates isn't an option.
This may not be the best way to do it, but I used ACF to add get_field( "region" ) and modified the code from this github. It's inserting the field where I need it, but the title is reading html as text. I have a <br/> in there now but would like to replace with <span class="">. Any idea why it's not being read as code?
live example >
Code:
function gp121028_filter_title( $title ) {
$substrings = explode( ' | ', $title );
$title = ( ! empty( $substrings[0] ) ) ? $substrings[0] . '<br/>' . get_field( "region" ) : $title;
return $title;
}
Wasnt able to solve it - built it manualy.

Use a Wordpress Page as a stylesheet?

I'd like to be able to edit my WordPress theme's CSS by using a Page in the backend instead of the default style.css file. I've seen it done before as a page template but I can't seem to figure it out myself.
I'm using WordPress version 4.7.2 on a Multisite. I want the CSS to generate on theme activation, hence using a Page.
I apologize in advance if this is an easy fix and open to other ways to accomplish this. Thanks!
I highly recommend using a plugin, such as https://wordpress.org/plugins/wp-add-custom-css/ or use the WordPress Customizer "Additional CSS" field.
However, to answer your question, this was easy since it's a modified way that I make some pages I load with ajax. You will need to create a page template and that requires FTP and a code editor to create the page and then push it your theme folder (hopefully a child theme or this template will go away when you upgrade).
I named my CSS page template: css-page.php
It requires only the opening php tag.
CODE for template css-page.php
<?php
/**
*
* Template Name: CSS page
* Not a recommended idea.
*
*/
header("Content-type: text/css; charset: UTF-8");
ob_start("compress");
//minify CSS
function compress( $minify ) {
/* remove comments */
$minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
/* remove tabs, spaces, newlines, etc. */
$minify = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minify );
return $minify;
}
//get the content
if (have_posts()) :
while ( have_posts() ) : the_post();
$content = get_the_content();
$content = wp_filter_nohtml_kses( $content );
echo $content;
endwhile;
endif;
ob_end_flush();
Then in your functions.php file in your child theme (or an include of that file or a custom plugin) paste in the following BUT change it where noted:
Change the value p='3134' to your page id where you're using this template. Change the id="something" to something else or remove it. Useful to have an id or a class for js manipulation.
For functions.php
//Get CSS page contents
function myprefix_page_css() {
echo '<link rel="stylesheet" id="something" href="'. site_url() .'/?p=3134" type="text/css" media="screen">';
}
add_action( 'wp_head', 'myprefix_page_css', 99 );

How do I get the name of the file that is being used to render the current page?

Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php.
I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.
I could do something with $_SERVER['PHP_SELF'], but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.
It can be printed in the Html source code like this:
add_action( 'wp_head', 'so_9405896_show_template', 999 );
function so_9405896_show_template() {
global $template;
echo '
<!--
TEMPLATE = ' . basename($template) .'
-->
';
}
Or for easier visualization, directly in the content with this:
add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );
function so_9405896_the_content_filter( $content )
{
if( is_admin() || !current_user_can( 'administrator' ) )
return $content;
global $template;
$the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = '
. basename( $template ) . '</strong><br />';
$content = sprintf( $the_templ . '%s', $content );
return $content;
}
Which results in:
As far as I've seen there's no built in option to enable such logging, only for errors.
I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.
I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.
A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.
I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live.
Simple and easy, if not so graceful.
For those looking for a newer answer:
<?php if ( is_user_logged_in() ) { global $template; echo basename($template); } ?>
This checks if a user is logged in, then only shows the template name. This can be handy if you need to hack your way on a live site without adding functions.

Is there a way of adding <strong> tags in Wordpress's description/tagline area

In Wordpress administrator -> Settings there's a tagline / description field:
For example:
<div id="tagline"><p><?php bloginfo( 'description' ); ?></p></div>
description: I'm a <strong>web</strong> and graphic designer
But it seems like it only allows text not HTMl tags.
Is there any way of doing this o I have to create a new field?
The option is run through a filter that replaces HTML entities, so even if you were to run a filter on that option(which is possible), WordPress replaces the HTML entities, meaning HTML wouldn't work...
For example, add this code and watch what happens.
add_filter( 'option_blogdescription', 'html_blog_description' );
function html_blog_description( $option_value ) {
$option_value = '<strong>Test text </strong>'. $option_value;
return $option_value;
}
There aren't any hooks i can see that specifically deal with this option and convert the entities, else i'd have ideas about unhooking that action from the description.
I think the only solution you really have is to run str_replace over your calls to fecth the blog description. This would mean updating all your calls for bloginfo( 'description' ) with a rountine that performs string replacement on the content.
eg.
$description = str_replace(' web ','<strong> web </strong>',get_bloginfo('description'));
echo $description;
Sure it's not ideal, but i can't currently see a more elegant way(though i'd be happy for someone to prove me wrong).
You could just change it in your stylesheet.
#tagline {font-weight: bold;}
Use html_entity_decode combined with get_bloginfo (instead of bloginfo). So replace:
<?php bloginfo( 'description' ); ?>
By:
<?php echo html_entity_decode( get_bloginfo( 'description' ) ); ?>
Note: Tested on WordPress 3.8.1.

wordpress plug-in or anything that will allow us to edit the meta tags for each individual post?

I researched at google but couldnot find anything.
Is there a plug-in or anything that will allow us to edit the meta tags for each individual post?
Appreciate helps! thanks.
You can also work with the meta description field in pages, single posts and post categories without a plugin by using a custom field called "description" and the built in category descriptions:
<meta name="description" content="<?php if (is_page()) { echo get_post_meta($post->ID, 'description', true);
} elseif (is_category()) { echo trim(strip_tags(category_description()));
} elseif (is_single()) { echo $title = get_the_title();
} else { echo bloginfo('description'); } ?>" />
Adapted from Stupid WordPress Tricks • Perishable Press
I suppose there are ; for instance, if you search for "meta tags" in wordpress' plugin lists, you'll get a few answers.
One of those answers is All in One SEO Pack -- which has good ratings, seems to work with recent versions of Wordpress, and has been recently updated.
Amongst the features, you'll see (quoting) :
Generates META tags automatically
You can override any title and set any META description and any META keywords you want.
Personally I use the excerpt field as the meta description if the page is a single post & the excerpt contains some text. Code for header.php:
<?php
if (is_single() && $post->post_excerpt != “”) {
$post = $wp_query->post;
$descrip = strip_tags($post->post_excerpt);
echo ‘<meta name=”description” content=”‘.$descrip.’”>’;
}
?>
I detailed the technique in this blog post.

Resources