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.
Related
I've written a simple Wordpress plugin that creates a custom post type (called a "river", it displays images of book covers).
I originally kept the templates for the rivers in my main theme file and followed the standard hierarchy by making a template named single-river.php, which works. I also showed the rivers interspersed with regular posts on the front page of my website by putting this inside the loop in index.php:
if (get_post_type() == 'post') {
get_template_part( 'template-parts/content');
elseif (get_post_type() == 'river' {
get_template_part( 'template-parts/river');
}
I'm adding some features to the plugin, and as part of that i'd like to move all the river-related code inside the plugin instead of having it scattered around.
Adding this code to my plugin works for displaying a single river on its own page:
add_filter( 'template_include', 'river_template', 99);
function river_template($template) {
if (get_post_type($post) == 'river' ) {
$file_name = 'single-river.php';
$template = dirname( __FILE__ ) . '/template/' . $file_name;
}
return $template;
}
What's the right way to specify a template part for rivers displayed on the front page/other list pages that display multiple posts, if i'm not using get_template_part in the loop? I've tried is_front_page() in the template_include filter without success and I'm not sure why that doesn't work.
Preface: I have developed a plugin, so editing theme files is out of the question.
My shortcode function:
/*** [leaderboard] shortcode with tournament_id input value ***/
function leaderboard_shortcode( $tournamentid ) {
ob_start();
include_once dirname( __FILE__ ) . '/leaderboard.php';
$output = ob_get_clean();
return $output;
}
add_shortcode( 'golf-deputy-leaderboard', 'leaderboard_shortcode' );
I tried adding in to relevant pages, however, if the shortcode is added to a page not controlled by the plugin - e.g. the home page - , the shortcode caches.
I guess my question is this: what options do I have to NOT cache the content of the shortcode, even if the page it is placed on, has caching enabled.
I have been racking my brain, the internetz, the WordPress Codex... all to no avail. I am open to any and all suggestions. Help me Stackoverflow, you're my only hope.
Cache functions start working before all possible filers&actions, and as shortcodes run inside the_content filter, it is impossible not to cache a shortcode only. I mean, when WP returns cached data, it returns static HTML data, no any theme&plugin's PHP runs in that case.
You need add that page (which has a shortcode in its content) to exclude list of your plugin. All popular cache plugins have such exclude field inside their settings.
Or another way is building custom cache solution which simply ignores caching then the_content contains any shortcode.
I have a Wordpress website that was about 5-6 custom post types. I have an archive.php and all the rest needed files. Most of them render good with my archive page but 2-3 render with my index.php.
What I tried:
I tried to make a page for each custom post, archive-{post-type} etc.. but that didn't work.
-
Does anyone have an idea about what might cause some of them to render good and only 2-3 to be problematic?
Go through the below following tips and snippet.
Remove if spaces on the top of your custom post archive file.
Go to Settings > Permalinks, change it to something else temporarily, press save, change it back to the original setting, press save again.
The below code snippet in your theme function.php
function get_custom_post_type_template($template) {
global $post;
if ($post->post_type == 'my_custom_post_type') {
$template = dirname( __FILE__ ) . '/archive-my_custom_post_type.php';
}
return $template;
}
//add_filter( "single_template", "get_custom_post_type_template" );//for single page
add_filter( "archive_template", "get_custom_post_type_template" ); //for archive
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/
I'm at an early stage of learning Wordpress (and shortcode), so bear with me:
To me, shortcodes seem like a swiss army knife of not having to use page-specific templates for everything. I like to build as many pages in the wysiwyg as possible, but often I would need some (reusable) php stuff for displaying stuff in a certain way.
Having googled a lot, it seems to me the way to do shortcodes is like:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
My question is, is it possible to put the html in a separate template-ish file? It seems wrong and verbose to put all this markup here, escape quotes, et.c. Like a template-file for a shortcode, to which the shortcode can pass some Data Transfer Object (or simply just some scoped variables). So: display in template-file, logic for finding data (to pass to said template-file) in shortcode-function (wherever it may be defined, functions.php, separate plugin, or something).
You can set-up views(php files) and then include partial views into those ones. Wordpress allows templates to be includes within other templates to ensure code reuse and its easily modifiable by child themes. You can use this function to include those
get_template_part( $slug );
However, in your case, the short code function needs to return the value to the caller function. So, this setup will not work.
For code that effects FUNCTIONALITY, put your code in a plugin.
For APPEARANCE, put your code in your theme's template files or funtions.php file.
Many beggining WP developers lump all their code into the theme's functions.php file, this is often the wrong place for it (if that code might ever get exported to another theme, for instance). Only put code specific to a specific theme in a theme's functions.php .
To get Wordpress to recognize your plugin, create a php file and start the file like this:
<?php
/*
Plugin Name: My Caption Shortcode Plugin
Description: A really cool plugin
*/
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
?>
Put this file in your plugins directory (usually, you should create a sub directory for each plugin). Plugins are usually held in /wp-content/plugins/ . Then you can activate or deactive the code as a plugin, when you go to the plugins tab in the admin menu.
Of course, this plugin won't do anything as is. Remember that plugin functionality should be hooked into Wordpress via action hooks, filters, and shortcodes. For a shortcode for instance, you'd use the function add_shortcode somewhere to let Wordpress know your function is a shortcode.