Wordpress PHP title creation - Solve errors on 'child theme' - php

I have a wordpress theme running the "static" side of my website (www.url.com/home), and I'm working outside of it for the App (www.url.com/app), but I'm using the wordpress header and footer in the App so as to mantain the look and feel.
The problem is that all pages in the /app directory are coded as a 404 Error. I can somewhat solve this using js to change the title of the page once the page loads, but that isn't a good solution (all search engines ignore those pages).
The wordpress part defines the title like this:
<?php
if ( is_plugin_inactive('wordpress-seo/wp-seo.php') ) {
bloginfo( 'name' );
}
?>
<?php
wp_title("|", true);
?>
I'm not very good with PHP. Can anyone offer a solution so that the title is set normally within the wordpress environment, but instead of automatically tagging the website as an error it will check if the url address starts with "url.com/app" and if it does, it will just title the page something ("Web app", for example).
Thanks, and sorry if the question is lame but I'm really new with PHP.

The correct way is to add a filter into your functions.php file within your theme. So:
wp-content/themes/[YOUR THEME]/functions.php
Add this to the end and then just change the "Your Page Title Here" string. It will add a filter and change any pages that include "/app" in the URI.
function set_title_if_slash_app( $title ){
$full_url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
if (strpos($full_url,"/app") !== false) {
return "Your Page Title Here";
}
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
add_filter( 'wp_title', 'set_title_if_slash_app');

Related

Timber/Twig and Wordpress Admin Integration

I am a beginner programmer and I got little problem.
The problem is that I can't link Custom page (page-product-all.twig) to Wordpress admin (can't find page-product-all on Wordpress admin.)
I have created custom twig page (page-product-all.twig)
I have created custom php file (page-product-all.php)
I have created a page in Wordpress admin panel (Page Product All)
But when I change the page on Admin (Page Product All), the page (page-product-all.twig) does not change.
Thanks in advance!
Take a look at page.php from the Timber Starter Theme:
<?php
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );
The Timber::render method on the last line loads page.twig as the default page template. However, Timber also checks for any twig files with the page- prefix followed by the name of a post (or in this case a page) with the code:
'page-' . $post->post_name . '.twig'
I really like this technique for handling custom pages because it prevents us from having to create custom page php files. This is especially helpful on sites with many pages.
If you want the page title to be "Page Product All" then your twig file will have to be page-page-product-all.twig. I sort of have a feeling you just want the page to be called "Product All" so in that case the twig file would remain page-product-all.twig and you will have to create a page called "Product All"
Now add a simple <h1>hello world</h1> to page-product-all.twig (I prefer <h1>hi mom!</h1>), preview the Product All page and voila. Custom page templates with Timber.

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.

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/

Can I put the excerpt of one page into the body of another dynamically?

The theme I am using has a page called 'home' whos body is displayed on the home page. I have another page set up called 'about' which is a detailed about page and quite long. I want there to be text on the homepage that is a short synopsis of the about page. I have this synopsis in the excerpt of the 'about' page. Is there a way for me to use that excerpt in the body of the 'home' page?
By doing it this way I can tell the client that all the about information is in one page instead of telling him to go to different pages to edit things that are directly related.
edit: I want to do it without editing the themes code so through the admins page editor
Rather than using "include", you could use the following to get the page excerpt of the about page and stick with native wordpress functionality:
$about = get_page_by_title( 'ABOUT_TITLE' );
$about_excerpt = $about->post_excerpt;
Then you can echo it into your theme template using:
<?php echo $about_excerpt; ?>
If you wanted to do this within the Wordpress backend, you would create a custom shortcode and wrap the above into a function to be called by that shortcode. Then you could put that shortcode wherever you wanted on any post/page.
EDIT:
Here is an example of doing this via shortcode so that you can use this for the About page or any other page with an excerpt within Wordpress:
add_shortcode( "Excerpt", 'nb_excerpt_shortcode' );
function nb_excerpt_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'for' => ''
), $atts) );
$page = get_page_by_title( $for );
$excerpt = $page->post_excerpt;
return $excerpt;
}
Then call it into action with [Excerpt for="About" /]
It sounds like something as simple as a PHP include of your 'about' file in your home file would give you what you are looking for ...
Place this code ( or an excerpt thereof ) in your home file, and make sure that your 'about.php' file is in the same folder...
<body>
<div id="about">
<?php include("about.php"); ?>
</div>
</body>
This would allow you to make changes to your about file without directly affecting your home, but the changes would be applied to both pages...

Wordpress content through php

Can someone please give me a hello world example for placing text in a wordpress content page through a custom php plugin?
I have been doing it through creating pages with a div id (through wp-admin) and loading content through extensive amounts AJAX/Javascript into that same id. I want to learn how to do this server side. ( It would make my life soooo much easier )
Use the Exec-PHP plug-in: http://wordpress.org/extend/plugins/exec-php/
Are you sure you want to use a plugin? Placing shorcodes in the functions.php file is the easiest route.
Example
In your functions.php file
function caption_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'class' => 'caption',
), $atts ) );
return '<span class="' . esc_attr($class) . '">' . $content . '</span>';
}
add_shortcode('caption', 'caption_shortcode');
In your blog post
[caption class="headline"]My Text[/caption]
The above would output. If your output is static, this would be the best way to go.
<span class="caption">My Text</span>
If you're sure you want to make it into a plugin, you can do that too. Simply putting this code in a file and putting it into your plugins directory will work. Make sure that you use the proper standards when defining the plugins (see link below).
The last link has both mentioned in the blog post and has a nice plugin that you can use for your own needs. That blog post explains how you can use the shortcodes in widgets as well.
Links
http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/
http://codex.wordpress.org/Shortcode_API
http://codex.wordpress.org/Writing_a_Plugin
http://aaron.jorb.in/blog/2010/02/wordpress-shortcodes-a-how-to-by-example/

Categories