I am trying to integrate wordpress with my previously installed platform.
I was able to edit most of the system as I wish, but I canĀ“t figure out how to intervene in what wordpress does before loading the template file.
What I want to add is a simple conditional statement that either load a page trough my previous cms, or loads normally wordpress.
However, I was unable to understand which function or file loads the components of the theme.
I understand wp uses template hierarchy, loading different files for different types of pages (posts, category page, and so on).
But what function is making this choices and actually loading the files?
Your question isn't clear enough in what you're actually trying to do but /wp-includes/template-loader.php is going to be of particular interest to you.
This is the file that loads the templates. Just to make it clear I'm not suggesting you modify this file I'm merely pointing out where the magic happens.
As soon as it's loaded it runs the action template_redirect. If you wanted to perform some logic then redirect here's where you'd do it. Here's an example that would go in your theme's functions.php:
function wpse_template_redirect() {
if ( condition_a() == condition_b() ) {
wp_redirect( 'url-to-redirect-to' );
exit;
}
}
add_action( 'template_redirect', 'wpse_template_redirect' );
At the bottom of this file there are conditionals which determine which template file is:
$template = false;
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :
elseif ( is_page() && $template = get_page_template() ) :
elseif ( is_category() && $template = get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template() ) :
elseif ( is_author() && $template = get_author_template() ) :
elseif ( is_date() && $template = get_date_template() ) :
elseif ( is_archive() && $template = get_archive_template() ) :
elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
elseif ( is_paged() && $template = get_paged_template() ) :
else :
$template = get_index_template();
endif;
/**
* Filter the path of the current template before including it.
*
* #since 3.0.0
*
* #param string $template The path of the template to include.
*/
if ( $template = apply_filters( 'template_include', $template ) )
include( $template );
return;
Related
It is possible, I list images of the current user and administrator, below is the code I am using.
It currently works normally for just the current user to list their images, but I wish the admin images could also be listed.
In other words, listing the images of the current user and the administrator, is it possible?
// Page media list only store images
add_filter( 'posts_where', 'attachments_wpquery_where' );
function attachments_wpquery_where( $where ) {
global $current_user;
if ( is_user_logged_in() && !current_user_can( 'administrator' ) ) {
// we spreken over een ingelogde user
if( isset( $_POST['action'] ) ){
// library query
if( $_POST['action'] == 'query-attachments' ) {
$where .= ' AND post_author='.$current_user->data->ID;
}
}
}
return $where;
}
I managed using this code, the conditional makes all the difference.
// Page media list only store images
add_action( 'pre_get_posts','users_own_attachments' );
function users_own_attachments( $wp_query_obj ) {
global $current_user, $pagenow;
if ( current_user_can( 'administrator' ) )
return;
if ( !is_a( $current_user, 'WP_User' ) )
return;
if ( ( 'edit.php' != $pagenow ) && ( 'upload.php' != $pagenow ) && ( ( 'admin-ajax.php' != $pagenow ) || ( $_REQUEST['action'] != 'query-attachments' ) ) )
return;
$author__in = array( $current_user->id, 1 );
$wp_query_obj->set('author__in', $author__in );
return;
}
i am trying to cleanup the file with phpcs --standard=Wordpress ../filename.php . But it seems phpcbf is unable to perform the action. So, can anyone share some help as how can i manually fix these doc comment and yoda errors. Attached is related chunk of code and the snip of the actual php file and the errors encountered. Any help would be highly appreciated.
<?php
function is_blog() {
return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag() ) && 'post' == get_post_type();
}
?>
<?php do_action( 'generate_before_footer' ); ?>
<div <?php generate_footer_class(); ?>>
<?php
do_action( 'generate_before_footer_content' );
// Get how many widgets to show.
$widgets = generate_get_footer_widgets();
if ( ! empty( $widgets ) && 0 !== $widgets ) :
// Set up the widget width.
$widget_width = '';
if ( $widgets == 1 ) {
$widget_width = '100';
}
if ( $widgets == 2 ) {
$widget_width = '50';
}
if ( $widgets == 3 ) {
$widget_width = '33';
}
if ( $widgets == 4 ) {
$widget_width = '25';
}
if ( $widgets == 5 ) {
$widget_width = '20';
}
?>
Above the is_blog() function, you'll need to add a docblock:
/**
* Function comment here.
*
*/
function is_blog() {
}
Yoda conditions are when you write the value to compare against before the variable you are comparing. So instead of:
if ( $widgets == 1 ) {
$widget_width = '100';
}
It wants you to write:
if ( 1 == $widgets ) {
$widget_width = '100';
}
Changing those things should get the file passing the checks.
Is it possible to add a page template located in a wordpress plugin folder instead of the theme folder so it is showing in the page attributes?
For a single page and archive i am using this code:
if ( get_post_type() == 'register' ) {
if ( is_single() ) {
if ( $theme_file = locate_template( array ( 'single-register.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = CONDOREG__PLUGIN_DIR . '/single-register.php';
}
}
if ( is_archive() ) {
if ( $theme_file = locate_template( array ( 'archive-condoleances.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = CONDOREG__PLUGIN_DIR . '/archive-condoleances.php';
}
}
}
return $template_path;
But for now it is not showing up in the attributes?
This is part of the wordpress code and I don't understand it:
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :
elseif ( is_page() && $template = get_page_template() ) :
elseif ( is_category() && $template = get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template() ) :
elseif ( is_author() && $template = get_author_template() ) :
elseif ( is_date() && $template = get_date_template() ) :
elseif ( is_archive() && $template = get_archive_template() ) :
elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
elseif ( is_paged() && $template = get_paged_template() ) :
else :
$template = get_index_template();
endif;
A colon can replace a curly bracket in PHP. So if I substitute the colons, I get this:
if ( is_404() && $template = get_404_template() ) {
elseif ( is_search() && $template = get_search_template() ) {
elseif ( is_tax() && $template = get_taxonomy_template() ) {
...
}
}
}
else
Makes no sense to me, because each elseif is missing its opening if.
Reggie,
colons in if/else statements in PHP : it's NOT about replacing braces
but a PAIR of braces.
Example :
if ($a) : doThis();
elseif ($b) : doThat();
else : doTheOther();
endif;
would become
if ($a) { doThis(); }
elseif ($b) { doThat(); }
else { doTheOther(); }
OR (since it's just one statement and not a block of statements)
if ($a) doThis();
elseif($b) doThat();
else doTheOther();
Reference : Alternative Syntax for Control Structures
As for this specific piece of code :
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
it translates to
if ( is_404() && $template = get_404_template() )
{ /* DO NOTHING */ }
elseif ( is_search() && $template = get_search_template() )
{ /* DO NOTHING */ }
Hint : The elseif statement does NOT include the other elseif statements. (like elseif ($a) { elseif($b) {} })
I want a post have the template of the parent category.. Is that possible? if yes, please guide me a bit. Or if any plugin is available, name it.
As of Wordpress 3.0, the logic in wp-includes/template-loader.php for selecting a template looks like this:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
$template = false;
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :
elseif ( is_page() && $template = get_page_template() ) :
elseif ( is_category() && $template = get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template() ) :
elseif ( is_author() && $template = get_author_template() ) :
elseif ( is_date() && $template = get_date_template() ) :
elseif ( is_archive() && $template = get_archive_template() ) :
elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
elseif ( is_paged() && $template = get_paged_template() ) :
else :
$template = get_index_template();
endif;
if ( $template = apply_filters( 'template_include', $template ) )
include( $template );
return;
endif;
Checking get_category_template() in wp-includes/theme.php` we see:
function get_category_template() {
$cat_ID = absint( get_query_var('cat') );
$category = get_category( $cat_ID );
$templates = array();
if ( !is_wp_error($category) )
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-$cat_ID.php";
$templates[] = "category.php";
$template = locate_template($templates);
return apply_filters('category_template', $template);
}
Assuming that your category is Foo, that it's slug is foo, and that the Foo category ID is 17, for a post that belongs to category Foo, Wordpress will check for the following templates in your theme and use the first one it finds:
category-foo.php
category-17.php
category.php
Thus, all you should need to do is to create a template named category-foo.php in your theme directory, and set your post's category to Foo, and that post will be rendered using the category-foo.php template instead of the default post.php template.
This mechanism for selecting templates has been present since Wordpress 1.5, though full list of template types has grown significantly over the years.
The Wordpress documentation for this can be found here.