Changing title of dynamic page in Wordpress - php

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/

Related

Post name and custom fields in Wordpress Shortcode

I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"].
As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode.
Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.
The same behavior I'd like to accomplish for 'custom field' - really similar situation.
I'd appreciate any help and any advice
Try to put this code to functions.php file in your theme:
function mymap_shortcode() {
echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');
And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.

Archive.php Category.php or Taxonomy.php - Which is correct for my situation?

I'm using Genesis framework and I have this page (http://staging.seedcreativeacademy.co.uk/short-courses/) showing the categories of my custom post type short_courses. I have changed the category name to course_type, by creating a new custom taxonomy.
This is how I want it to work so far (styling needs sorting out admittedly!) Im also using CPT UI plugin.
Now, when I click through to a category, is displays each 'Course in a nice masonry block as you will see here: http://staging.seedcreativeacademy.co.uk/course_type/digital-marketing/
However, I dont want this pages to look like this and I've tried adding custom template for the following:
Archive-short_courses.php & taxonomy-short_courses.php
Archive-course_type.php & taxonomy-course_type.php
But it doesnt seem to alter the layout at all...
Once I pass this hurdle I will want to alter the single.php page for these short courses, but I thought I would start with this first.
Im not sure if genesis blocks this and sets a site wide default? I know if sets a site wide default for the archive settings but I cant find anything about a template, plus i dont know if I shoujld be searching for tutorials on archive.php pages, category.php pager or taxonomy.php pages...
Can someone help me clarify things please?
course_type is a term name, not taxonomy name.
So, these are correct for your case:
category-course_type.php (category-{slug}.php is correct format. So check if course_type is correct slug of that category)
single-short_courses.php
Just in case, try reload permalinks via Settings->permalinks->save after making these changes.
Looks like your theme or some plugin is adding masnory class to body tag, which then is styled by your child theme. You need to filter that class out of your body tag and then might styling goes to non-masonary styling.
Add following code to your taxonomy-course_type.php file, and also make sure you have genesis(); call as the last thing in the template.
add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
{
foreach($wp_classes as $key => $value)
{
if ($value == 'masonry') unset($wp_classes[$key]);
}
return $wp_classes;
}
Above could should be in custom taxonomy template, which also have genesis(); as last line.

Adding a PHP page to Wordpress via Plugin

Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.

Insert variable into Wordpress page title for individual page

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)

Using variables in the URL on wordpress menu link

I am using wordpress wp-admin to create the menu. It is under Appearance/menu.
I have a link that points to /members/ but what I really need is a link to /members/$logged_user...
For example /members/user_1 or /members/user_2.
How can I do that?
I dont know if it is important, but I am using buddypress plugin.
I have written a short script for adding dynamic buddypress links in wordpress menu. Hope it helps
I have added custom links in wordpress menu replacing username in link with --username--
example
http://website_name.com/members/--username--/messages/
then add this code in function.php
add_filter( 'nav_menu_link_attributes', 'menu_override', 10, 3 );
function menu_override( $atts, $item, $args ) {
$user = wp_get_current_user();
$newlink = str_replace("--username--", $user->user_login, $atts[href]);
$atts[href] = $newlink;
return $atts;
}
The default menu doesn't really have that option. But you can make your own walker function, searching for the keyword and replacing it with the current user.
See http://codex.wordpress.org/Function_Reference/wp_nav_menu#Using_a_Custom_Walker_Function
But it would probably be faster and more manageable to just place the link outside the menu call with some static html and the needed php.
You can use the BuddyPress Custom Profile Menu plugin to get close to this functionality, otherwise you will have to write custom code that uses actions/filters of wp_nav_menu().

Categories