How to add <title> to Custom Pages in WP's function.php? - php

I'm using a wordpress theme that creates custom pages.
However, when SEO Yoast is used to make custom title and meta descriptions, all the custom pages have the same title tags as index.php (the homepage). This is a red flag for google webmaster tools obviously.
add_action('theme_query', 'theme_page_query');
function theme_page_query() {
global $wp_query;
$act = get_query_var( 'act' );
if ($act == 'new-wallpapers') {
query_posts('orderby=post_date&order=DESC&paged='.get_query_var('page'));
} elseif ($act == 'popular-wallpapers') {
query_posts('meta_key=theme_hit_download&orderby=meta_value_num post_date&order=DESC&paged='.get_query_var('page'));
} elseif ($act == 'random-wallpapers') {
query_posts('orderby=rand&paged='.get_query_var('page'));
} elseif ($act == 'recent-downloads') {
query_posts('meta_key=theme_hit_download_time&orderby=meta_value&order=DESC&paged='.get_query_var('page'));
}
}
function theme_page_title($title) {
if (in_array(get_query_var( 'act' ), array(
'new-wallpapers',
'popular-wallpapers',
'random-wallpapers',
'recent-downloads',
))) {
$title = ucwords(str_replace('-', ' ', get_query_var( 'act' )));
}
return $title;
}
add_filter( 'wp_title', 'theme_wp_title', 10, 2 );
function theme_wp_title( $title, $sep ) {
global $paged, $page, $wp_query;
if ( is_feed() ) {
return $title;
}
if (in_array(get_query_var( 'act' ), array(
'new-wallpapers',
'popular-wallpapers',
'random-wallpapers',
'recent-downloads',
))) {
$title = ucwords(str_replace('-', ' ', get_query_var( 'act' ))) . " {$sep} ";
}
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title = "$title $sep $site_description";
}
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 ) {
$title = "$title $sep " . sprintf( __( 'Page %s', 'theme' ), max( $paged, $page ) );
}
return $title;
}
I have tried searching, but I cannot find a step-by-step guide and I am not good in PHP. Any help would be much appreciated.

You can do this in three steps by
1.Add a new input box to the admin pages for creating and editing new Posts/Pages
2.Save the the value of that input box when that Post/Page is saved
3.Echo out a new title as part of the wp_head() function
here is the detailed explanation..Hope it helps ..Thanks

Related

How do I create a woocommerce product title based on whether a variable exists or not using function.php?

I need some help with a woocommerce website with a function.php file that I edit within a plugin called WP All Import. It's linked to a plugin that imports csv & xml files.
In WP All Import I set the import title as [product_title({product_name[1]}, {size[1]}, {colour[1]})] and it picks up the items from an import.
What I'm trying to do is set the product name as a combination of variables. So in the example below, if both $size and $colour are empty I want the product name to just be $name. If $size is empty, then $name - $colour. If $ colour is empty then $name - $size. And then if they all have values $name - $colour - $size.
function product_title($name, $size, $colour)
{
$newName = $name;
if (strpos($size, "")) {
if (strpos($colour, ""))
{
$newName = "$name";
}
}
else if (strpos($size, ""))
{
$newName = "$name - $colour";
}
else if (strpos($colour, ""))
{
$newName = "$name - $size";
}
else
{
$newName = "$name - $colour - $size";
}
}
At the moment this is leaving the titles as blank. Where did I go wrong?
I've had to do this not so long ago. It's pretty tricky to understand. In short you can't use pre_get_post because the terms are not set yet. You must use save_post with wp_update_post, but there is a few thing to understand like the infinite loop case.
Don't hesitate to adapt it. I guessed that colors and sizes are custom taxonomies, just make sure the slugs matches. Modify it and have fun.
<?php
add_action( 'save_post', 'worker', 10, 3 );
function worker( $post_id, $post, $update ) {
// ... if not the admin side or if user can't edit post, then bail
if ( ! is_admin() || ! current_user_can( 'edit_post', $post_id ) )
return;
$worker = ( object ) [
'post_type' => 'product', // ... set our taxonomy in your case, "product"
'taxonomies' => [ 'colour', 'size', ], // ... set the taxonomies that we want to use in our title
];
// ... if not a post.php page or post-new.php page, then bail
$base = [
'post.php',
'post-new.php',
];
if( ! in_array( filter_input( INPUT_SERVER, 'REQUEST_URI' ), $base, true ) && $post->post_type != $worker->post_type )
return;
// ... do not update title if autosave
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// ... fetch our terms and join them
$terms = join( ' - ', wp_list_pluck( wp_get_object_terms( $post_id, $worker->taxonomies ), 'name' ) );
// ... define our title structure
$self = $post->post_title . $terms . '#' . $post_id; // ... You should leave the post_id as a unique identifer, so you don't end up with 2 product with the same title
// ... https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
remove_action( 'save_post', 'worker' );
wp_update_post( array(
'ID' => $post_id,
'post_title' => esc_attr( $self ),
'post_name' => sanitize_title( $self ),
) );
add_action( 'save_post', 'worker', 10, 3 );
}; ?>

Wordpress (WP ecommerce) add publish/unpublish to row actions

How can I add a publish/unpublish quick button in wordpress admin post grid (like Edit, Quick Edit, ... on the screenshot)
]1
This is a screenshot from wp-ecommerce plugin product grid (it is very similar to wordpress native post grid).
For example the code for the duplicate button from wp-e-commerce plugin is:
function wpsc_action_row( $actions, $post ) {
if ( $post->post_type != "wpsc-product" )
return $actions;
$url = admin_url( 'edit.php' );
$url = add_query_arg( array( 'wpsc_admin_action' => 'duplicate_product', 'product' => $post->ID ), $url );
$actions['duplicate'] = '' . esc_html_x( 'Duplicate', 'row-actions', 'wp-e-commerce' ) . '';
$publishUrl = 'edit.php?wpsc_admin_action=toggle_publish&product='.$post->ID;
if($post->post_status == 'publish') {
$published = 'Unpublish';
} else {
$published = 'Publish';
}
/* $actions['publish_toggle'] = '' . esc_html_x( $published, 'row-actions', 'wp-e-commerce' ) . ''; */
return $actions;
}
add_action( 'wp_ajax_update_featured_product', 'wpsc_update_featured_products' );
add_action( 'admin_init' , 'wpsc_update_featured_products' );

Is there anyway to add rel="next"/rel="prev" into "Page Template" with WordPress SEO by Yoast?

Our site is currently using "WordPress SEO by Yoast"
rel="next" and rel="prev" is working fine on category and archive page, however in page template that we created, rel="next" and rel="prev" is not showing.
(This page template also has pagination)
Our website structure => we have "Article" Post type
in Article we have category
Credit card
Cash card
Loan
etc.
As I want the url to be www.sitename.com/loan without having ../category/loan
I created 'Page' called 'Loan' and using page-loan.php as page template to query Post type 'Article' category 'Loan'
I want to have rel="next" and rel="prev" appear in this page template as well
I wonder is there anyway to use WordPress SEO by Yoast to do it?
or is there anyway to modify below script in the plugin to make rel="next" and rel="prev" appear in Page template as well?
the script I found in the plugin
public function adjacent_rel_links() {
// Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
/**
* Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
*
* #api bool $unsigned Whether or not to rel=next / rel=prev
*/
if ( is_home() && function_exists( 'genesis' ) && apply_filters( 'wpseo_genesis_force_adjacent_rel_home', false ) === false ) {
return;
}
global $wp_query;
if ( ! is_singular() ) {
$url = $this->canonical( false, true, true );
if ( is_string( $url ) && $url !== '' ) {
$paged = get_query_var( 'paged' );
if ( 0 == $paged ) {
$paged = 1;
}
if ( $paged == 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
// Make sure to use index.php when needed, done after paged == 2 check so the prev links to homepage will not have index.php erroneously.
if ( is_front_page() ) {
$url = wpseo_xml_sitemaps_base_url( '' );
}
if ( $paged > 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
if ( $paged < $wp_query->max_num_pages ) {
$this->adjacent_rel_link( 'next', $url, ( $paged + 1 ), true );
}
}
}
else {
$numpages = 0;
if ( isset( $wp_query->post->post_content ) ) {
$numpages = ( substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1 );
}
if ( $numpages > 1 ) {
$page = get_query_var( 'page' );
if ( ! $page ) {
$page = 1;
}
$url = get_permalink( $wp_query->post->ID );
// If the current page is the frontpage, pagination should use /base/
if ( $this->is_home_static_page() ) {
$usebase = true;
}
else {
$usebase = false;
}
if ( $page > 1 ) {
$this->adjacent_rel_link( 'prev', $url, ( $page - 1 ), $usebase, 'single_paged' );
}
if ( $page < $numpages ) {
$this->adjacent_rel_link( 'next', $url, ( $page + 1 ), $usebase, 'single_paged' );
}
}
}
}
/**
* Get adjacent pages link for archives
*
* #since 1.0.2
*
* #param string $rel Link relationship, prev or next.
* #param string $url the un-paginated URL of the current archive.
* #param string $page the page number to add on to $url for the $link tag.
* #param boolean $incl_pagination_base whether or not to include /page/ or not.
*
* #return void
*/
private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
if ( $page > 1 ) {
$url = add_query_arg( 'paged', $page, $url );
}
}
else {
if ( $page > 1 ) {
$base = '';
if ( $incl_pagination_base ) {
$base = trailingslashit( $wp_rewrite->pagination_base );
}
$url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
}
}
/**
* Filter: 'wpseo_' . $rel . '_rel_link' - Allow changing link rel output by WP SEO
*
* #api string $unsigned The full `<link` element.
*/
$link = apply_filters( 'wpseo_' . $rel . '_rel_link', '<link rel="' . esc_attr( $rel ) . '" href="' . esc_url( $url ) . "\" />\n" );
if ( is_string( $link ) && $link !== '' ) {
echo $link;
}
}
First of all, you have to make a backup of your database. You can achieve this on phpmyadmin or with some plugin that do the backup for you.
Next, you should do the url thing. You can remove the category path from categories in the page "Search Appearance - Yoast SEO", click on tab Taxonomies, then remove the Category URLs.
On the pagination issue, you could manually input the links on each page or use a php function to get all the links of post type articles like this:
wp_list_pages(array(
'child_of' => $post->post_parent,
'exclude' => $post->ID));

Wordpress how to change Page title based on My post title

I want that wordpress should display title from selected post and selected page.I am using buisness news theme. Is their any setting available or I need to change in code I am new to wordpress. I found title code in header.php
<title><?php
//Print the <title> tag based on what is being viewed.
global $page, $paged;
// Add the blog name.
bloginfo( 'name' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) echo " | $site_description";
// Add a page number if necessary:
if ( $paged >= 2 || $page >= 2 )
echo ' | ' . sprintf( __( 'Page %s', 'business-news' ), max( $paged, $page ) );
?></title>
But when I select particulate post it shows only site name in title. Where I am missing?
Try this:
<title>
<?php single_post_title(); ?>
</title>
manage using add_filter wp_title() function
<title><?php wp_title(''); ?></title>
If u want change using like this in function.php
function theme_name_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', 'theme_name_wp_title', 10, 2 );
You can simply use simply :
<title><?php wp_title(); ?></title>
For more help go to here

How to generate custom page names in wordpress?

I lost my old website's code and am trying to recreate a function I had created ages ago.
I have custom pages in my Wordpress blog: about, work, contact, news as well as posts and subpages.
I am trying to manipulate the value of <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1> to display those different page tiles for those pages and their children so that it would look something like this:
if( $show == 'name' )
{
if( is_page('work') ) $output = 'Work';
if( is_page('contact') ) $output = 'Contact';
if( is_page('about') ) $output = 'About';
}
return $output;
And as for all the rest of the pages, I just want it to display the default site value so somewhere along the line there would be an
else $output = "Site Name";
So I kind of understand the logic because I have done it in the past but cannot remember how the syntax went.
Could anyone refresh my memory?
You could put the following in your functions.php file:
function so20527793_sitename()
{
$output = get_bloginfo( 'name' );
if( is_page( 'work' ) ) $output = 'Work';
if( is_page( 'contact' ) ) $output = 'Contact';
if( is_page( 'about' ) ) $output = 'About';
return $output;
}
Usage:
<h1 class="site-title"><?php echo so20527793_sitename(); ?></h1>
Well I did not get much help in the end so I had to figure it out by googling and combining functions. Here is the result:
function is_child($page_id_or_slug) {
global $post;
if(!is_int($page_id_or_slug)) {
$page = get_page_by_path($page_id_or_slug);
$page_id_or_slug = $page->ID;
}
if(is_page() && $post->post_parent == $page_id_or_slug ) {
return true;
} else {
return false;
}
}
function Custom_PageName(){
$output = get_bloginfo( 'name' );
if (is_page('about') || is_child('about')) $output = 'About title';
if (is_page('work') || is_child('work')) $output = 'Work Page';
if (is_page('contact') || is_child('contact')) $output = 'Contact';
return $output;
}

Categories