Insert variable into Wordpress page title for individual page - php

I have a photo gallery page that is using a single page in Wordpress.
The gallery display is dynamic, and relies on a URL parameter. The parameter itself is an integer of the relating wordpress page ID. (as that's the page it's about).
Here is an example of a gallery URL:
http://www.bellavou.co.uk/gallery/?pid=238
For SEO purposes, I'm trying to get away from every possible Gallery URL combination to have the page title 'Gallery', as is set in the Wordpress page settings.
I've been trying this, which has been mentioned a few times:
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
But as I'm also using Yoast SEO plugin, I think this is over-writing it (even though I uncheck 'Force title rewrite', and keep the page title field blank). Not sure why the above function doesn't seem to work.
Within the gallery page, I'm able to show the h1 title properly, passing PHP Variables into the code, but I want to do the same to the page title, but in a way that can be done directly on the page template.

Why is it that whenever I post a question, I seem to find the answer very soon afterwards?
I read a post saying that it helps to put a wp_title function before the wp_header call, which is what I tried, and that seems to work fine.
I can edit the page title just for an individual page using the PHP page template, and using this:
$procedure = isset($_GET['pid']) ? $_GET['pid'] : '';
add_filter( 'wp_title', 'wp_title_gallery', 100 );
function wp_title_gallery($title) {
global $procedure;
if(!$procedure) {
$title = the_title();
} else {
$title = the_title(get_the_title($procedure).'&nbsp');
}
return $title;
}
get_header();
$procedure gets the URL parameter, and then the function takes that ID, and gets the associated page from it, and then prints the title of the page, into the page title hook. Lastly, the WP header is called.
(Just incase anyone comes here looking for a solution)

Related

How do I hide referrer link in woocommerce product

I have a link that is in the short description of a woo-commerce product. This link type is there in every product I have (different of course).
I'll need a script to automatically change the links. These are the attempts I tried so far:
WP-HideRefer
I have installed the plugin (https://wordpress.org/plugins/wp-hiderefer/), did not work. Did not change any links.
Added scripts
I used the script provided in https://anonym.to/ and put in at the end of my footer.php (before ), did not work.
Tried many other Wordpress hide referrer plugins, did not work.
Up until this point, I'm lost. I am not sure.
TL:DR
Appending text in front of every external link (link in short desc/normal desc of woocommerce product EXCEPT own website domain/link.
https://www.google.com/ is on my woocommerce product.
When user loads the product, he sees https://anonym.to/?https://www.google.com/ instead.
(https://anonym.to/? + link)
This code snippet will replace all URLs on a WooCommerce product page in both the excerpt (short description) as well as the longer description/content if you want.
It uses a regex to replace the URLs.
function so61548594_anonymize_urls($description) {
$prefix = 'https://anonym.to/?';
$description = preg_replace(
'#(<a.*?)href=["\']([^"\']+)["\'](.*?>[^>]*</a>)#i',
('$1href="' . $prefix . '$2"$3'),
$description
);
return $description;
}
add_filter('woocommerce_short_description', 'so61548594_anonymize_urls');
add_filter('the_content', function ($description) {
if (function_exists('is_product') && is_product()) {
return so61548594_anonymize_urls($description);
}
return $description;
});
Let me know if it works for you.

WordPress replace page with a plugin

I am trying to make a plugin that will display custom page when a parameter is present in the URL. For example, let's say that the URL of a post is localhost/wp/2018/03/22/postname/, it displays post normally as expected. What I want to achieve, is when I specify URL as localhost/wp/2018/03/22/postname/?param=1, I want to display page that I will generate with the plugin. I made a function that works when the parameter is present and it echoes some example content, however, it merges with what WordPress normally displays. So when I put the parameter into URL I get a regular page with post and my content somewhere in the middle. I want to display only the page that i generate from scratch, from <html> to </html> with my plugin. How can I do that?
There is a filter in WordPress called template_include this filter is executed before any output is displayed on the WordPress generated page.
You can override the current template via this filter as soon as you find the parameter and value in the URL. E.g. below is a filter I was using in a project to override archive & single template for a certain CPT:
function em_templates($template) {
if(get_query_var('post_type') == 'xyz' ) {
return FMM__DIR__ . '/templates/archive-xyz.php';
}
return $template;
}
add_filter('template_include', 'em_templates', 1, 1);
You can adopt this logic for parameter and use this WordPress filter to take over the template process and display yours own.

Alter WordPress post title and body in view mode only

I am looking for the way how to alter a WordPress post title and body when it is displayed to a normal user. But not in any other context, ie in admin module.
The task at hand is to fetch a query parameter, carry out some calls to external webservice and database operations and replace some text in both title and body. The <title> tag should also contain the changed title.
I do not look for a plugin solution, but rather were I should include a php file with all the code that does all the necessary processing.
My expectation was that I don't have to deeply meddle with the theme files. Of course, I could inject my code in the respective theme's page.php or content.php file. But I'd like to do it in less intrusive way, for example by including my functions_custom.php file in the top of functions.php, and this file would contain a collection of functions and add_filter() calls to hook those functions on certain events. Is that possible to achieve my goal that way?
This is my first attempt:
<?php
function alter_title( $title, $id = null ) {
return 'Altered title: ' . $title;
}
add_filter( 'the_title', 'alter_title', 10, 2 );
?>
This causes change in all the post titles in all contexts, including the post list in the Dashboard. But I need it to be altered only in view context (this also does not influence the text used in the <title> tag). Perhaps I can sniff this context somehow, and engage the alteration only when the view mode is detected?
While ago I needed to do similar thing and my fast and short workaround was to have an "API" (
Created a template for that specific post type and using PHP generated a post ID indicator:
$post = get_post(); echo "var post_id = '".$post->ID."';";
I created a custom API that "takes" $_POST request with "post_id" param and it would fetch adequate/custom (in your case a content that will be shown to the user) content from database for the post with the post_id from $_POST request.
Then for every post there would be an ajax request to that API which sends post_id and fetches all the needed info (in your case post title and body) and then using eg. jQuery
.append() would populate post title with response post title and body too, respectively.
Also, if you'd like to avoid a extra request than you should try to generate that 'user-visible' content with PHP. That would mean you need to collect the content before loading the page and echoit in the place of default content.
I suppose you should create a function in functions.php that takes an $post->ID and fetches title and body from the database. Then you should call that function on every loop and single post template, if you understand me.
is_admin() will return true if you are viewing an admin page.
So you could do this:
function alter_title( $title, $id = null ) {
if(is_admin()){
return $title;
}else{
return 'Altered title: ' . $title;
}
}
add_filter( 'the_title', 'alter_title', 10, 2 );

How to display page content in custom theme in wordpress?

I have been searching for this a few hours now and I might just overlook it or asking the wrong questions in google, but I would really like to know how I can get the content I type in e.g. the About page in the WordPress dashboard, to display on my page.php.
You can get the content of any arbitrary page like this:
$page = get_page_by_title('About');
I'm grabbing the page by the title, you can also get it by ID or URL path
You can then display it by outputting the post_content property of the post object. Note that if you need it to be formatted, you'll need to apply the the_content filter:
echo apply_filters('the_content', $page->post_content);
If you need to show a specific post/page by id, Wordpress has the function
<?php get_post( $id, $output, $filter ); ?>
Documentation here: https://codex.wordpress.org/Function_Reference/get_post

Changing title of dynamic page in Wordpress

Does anyone know of a way to change the title of a page that is created dynamically in Wordpress? The pages in question are created dynamically via a plugin, and do not appear in the list of pages or Wordpress SEO in the admin section.
Here is an example of a page I would like to change the title of: http://www.forestlakechevrolet.com/inventory/New/
The inventory tool is maintaining the inventory of two dealerships, but only one is featured on this site. So it uses an SEO title based on the makes available at both dealerships.
Essentially, I am wondering if there is a function that I could add that would say "If URL is ... then force page title to be ***."
Does anyone know how to do this?
Thank you,
Jared Wilcox
Just use the wp_title filter
Add something like this to your theme's functions.php file:
<?php
function assignPageTitle( $title ){
global $post;
if ($post->post_name === "test") {
$title = "Title goes here";
}
return $title;
}
add_filter('wp_title', 'assignPageTitle');
?>
You may also need to change your theme's header file if the wp_title call is adding the Site Name.
BTW, WordPress questions are better asked on https://wordpress.stackexchange.com/

Categories