Wordpress First Index PHP location - php

What is the first php file wordpress goes to to determine what page, document or URL to load. I have some PHP I want executed to determine what happens when certain URL's are entered. At the moment it's in the 404.php for want of a better place.
This however is not ideal. The index.php in theme is not referenced so what is?

It's one of the many pages loaded from wp-settings.php. Anyway, you don't have to go that deep into the core structure and spoil WP's native functionality I think, you can do whatever you prefer in theme functions.php, so you can get a page name just by doing something like this in functions.php:
$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}

Related

Create a second single.php file

I'm trying to create a second single.php for my WordPress theme.
The first single is used with get_permalink() but it's impossible to create another single2.php
How is this feasible?
I'd like to use the single2.php the same way as the regular single.php
I've seen some tutorials but every time they associate the single2.php with the creation of WP categories and I know it's doable without it. I just don't know how.
Open single.php in your HTML editor. Delete the contents of single.php and place the following code in it. Substitute the number 3 for the Category ID # you noted.
<?php
$post = $wp_query->post;
if ( in_category('3') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
You should have a look at the Wordpress Template Hierarchy, it could give you tips to do that :
You could create a page-{slug}.php or a page-{id}.php file for example, but it's not the nicest way as it's only for a specific slug or specific id.
I'd recommend to create a template file, where you can create your page. This template can be reuse for several pages !

Wordpress plugin - how to disable js/css per page

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

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.

One page wordpress issue with get_template_part and get_footer

What I'm trying to do is convert my one page design in wordpress and i thought it would be nice to be able to edit, add and modify different part of the page in seperated pages. The one page website will be ordered by a menu (with id main).
Following the wp-codex i used get_template_part, it should work properly because it should:
Load a template part into a template (other than header, sidebar, footer)
get_header gets skipped but get_footer gets excecuted and the site is rendered incorrectly.
front-page.php
$pages = wp_get_nav_menu_items('main');
global $post;
foreach ($pages as $post) {
$post = get_post($post->object_id);
if($post->post_type != "page") continue;
setup_postdata( $post );
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? null : $post->post_name;
get_template_part('page', $postName);
}
wp_reset_postdata();
Is this a bug? Or did I do something wrong? What is the best way to accomplish this?
if your get_template_part() is not working, then the problem may be there is no file named as page.php in theme, check for this file.
For what it's worth, I often skip a some of the WP helpers and work more directly, but not rogue. Here is a snippet from my library :
/** Get core data for a WP post/page using ID
* #param $id : wp post/page ID
* #return array
*/
function wp_get_single_post_basic($id){
$post = get_post($id, ARRAY_A);
$ret = array();
$content = $post['post_content'];
// FILTER via WP
$content = apply_filters('the_content', $post['post_content']);
$ret['title']= $post['post_title'];;
$ret['content']= $content;
$ret['link'] = $post['post_name'];
$ret['edit_link'] = 'edit';
return $ret;
}
That function breaks down the content in a very easy to manage manner and generates the edit link (build bool for that in calling script). Also formats content.
So grab and sort the IDs you want and run this in the loop over IDs for your one page. Then all of the content will be isolated by page/post/whatever (custom taxonomy FTW).
If you had the IDs ready and sorted plus the html/css ready to go I could drop this function in and work your one page to complete in under an hour. For production line work this type of helper is great -- also perfect for sites where you want a specific post/page to be placed in some weird way in some weird place outside of the loop.
If you have a problem w/script let me know, I have not used it in a few months, wrote it a few years ago...
I think you have a problem using the global $post and storing also the $post variable in the foreach loop from the menu objects. I think probably that's the cause of the error with the template part calling.
I would recommend you to remove the global $post declaration, the reset_postdata and also the setup_postdata since you aren't using the global in your loop, you're just naming it.
You're getting the $post object from the menu so it appears that you don't need the global or the other functions that are mostly used when you want to use "Loop Style" template tags without a post_id, like the_permalink() or the_title().
I ended up copying the default page.php template as page-page.php and loaded it like this:
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_name;
Then in page.php just loaded page-page.php

Wordpress Conditional Sidebar Section Based off of Page ID

Goal - have a section of my sidebar that is conditional depending on what page I'm on. All child pages of the selected page should also show the conditional section.
There are at least two ways to do this - 1) have all the conditional hooks and resulting code in one sidebar file and, 2) have multiple sidebar.php documents and call them based off what pages are being visited.
I've pretty much failed at both ways so far... :( I'm working on the second method right now though, because I think it might be more flexible long term. Anyway, in my page.php I replaced:
<?php get_sidebar() ?>
With
<?php
if (is_page(1997) || $post->post_parent) {
get_sidebar('sidebar-test-may12.php')
}
else { get_sidebar()
} ?>
With the hope of testing the method. What I want to happen is that if the page is page id 1997 or any of its child pages please show sidebar-test-may12.php. Otherwise show the regular sidebar. With the new code the whole page goes blank. Any suggestions or solutions for me? Seems like a common problem, but I haven't been able to figure it out yet. Thanks!
You have a few problems with your code example:
1) $post->post_parent is not being checked against anything else, so it will only return true if it is a child page (not necessarily the child of the page you want to target)
2) get_sidebar() is being called incorrectly. If you want to get 'sidebar-test-may12.php' from your theme folder, you need to call get_sidebar('test-may12')
3) You're missing semicolons after your function calls
So your code should look like this:
<?php
if(is_page(1997) || $post->post_parent == 1997) {
get_sidebar('test-may12'); //get sidebar-test-may12.php
}
else{
get_sidebar(); //get sidebar.php
}
?>
Let me know if that helps.
UPDATE: Bear in mind, $post->post_parent does NOT get the top-most ancestor ID of a child page. If you want to grab the top-level ID regardless of depth, consider doing this:
<?php
$ancestors = get_ancestors(get_the_ID(), 'page');
if(is_page(1997) || end($ancestors) == 1997)
get_sidebar('test-may12'); //get sidebar-test-may12.php
else
get_sidebar(); //get sidebar.php
?>
POSSIBLE SOLUTION: Building off your example and my proposed ancestry check, one thing you can do is have your template check to see if a special sidebar exists in your theme based on the parent page's slug. This way, if you decide a particular page needs a special sidebar for it and all of its children/grandchildren/great-grandchildren/etc. you just need to add it into your theme with a name of 'sidebar-{parent_slug}.php'. So:
<?php
$id = get_the_ID();
$ancestors = get_ancestors($id, 'page');
$top_page = $ancestors ? get_page(end($ancestors)) : get_page($id);
if(locate_template('sidebar-'.$top_page->post_name.'.php'))
get_sidebar($top_page->post_name);
else
get_sidebar();
?>
This way, you don't need a ton of conditionals to decide which Sidebar file to load on a general Page template.
Use some error trapping to show what's going on with your php, either toggling an error log in .htaccess of a simple widget: http://wordpress.org/extend/plugins/error-log-dashboard-widget/
I've hardcoded logic for sidebars, but I've also used WordPress › Widget Logic « WordPress Plugins for my own and client sites. It's easy to use with a simplified version of WP conditionals, like is_category and is_page ('101')
For someone else tackling this same problem -- there is a new plugin out that solves this problem very well. It even makes creating the sidebars easy. You can check it out here:
http://wordpress.org/extend/plugins/custom-sidebars/

Categories