Using include() instead of get_footer() - php

Simple question. In a Wordpress template, is there a reason I shouldn't use include() and use get_footer() ?
Let's say I want to have a sub folder in my template directory with all my components included. This seems to work okay but maybe I'm missing something.

You shouldn't use include() in your Wordpress theme at all.
The reason is that Wordpress' architecture is very pluggable - throughout core, plugins and themes, most components can interact with other components through actions and filters.
While get_footer() might seem simple, the functions that it runs allow parts of your theme to be overridden. In this case, locate_template() allows a child theme to ship with a footer.php file in order to override the one set up by your main theme.
In addition, get_footer() itself allows the flexibility of including multiple footer files in your Wordpress theme so that you can call a slightly different footer on a particular template if you need to (by way of eg. get_footer('alternative') to call footer-alternative.php - which is then also overridable by a child theme).
It's worth noting that you should also ensure your footer template has a call to wp_footer() in it - ideally directly before the </html> tag. This goes back to what I mentioned above about actions and filters: in this case, any functions hooked to the footer by plugins or Wordpress core (such as script includes) will get run as intended (incidentally, get_footer() itself also runs the get_footer action, which allows eg. a plugin to override which footer template is called - this is thus another reason to use get_footer()).
Finally, in relation to not using include() in your theme at all, if you find yourself needing to include another template file that isn't a header or footer, get_template_part() exists for that. While it sometimes may seem easier to just use native PHP functions rather than the wrappers the Wordpress architecture provides, in the end doing it the 'Wordpress way' means that your theme will interact better with plugins and future versions of Core, plus be more maintainable by others. And, you'll probably help avoid causing weird bugs for yourself too!

get_footer() (click link for source) doesn't do anything further in the background rather than locate and include the file anyway.
get_footer() then calls locate_template() which then calls load_template() which only does: require_once($template_file)
It's up to your preference but would recommend using get_footer() in case something does change in the future

Related

Adding jQuery code in Joomla

I need to add this code to Joomla please
$(".item-204").append('<ul class="nav-child unstyled small"><li class="item-205"><a href="index.php?option=com_content&view=article&id=9" >رؤيتنا</a></li>');
what is the best way?
One way to do this would be to use Regular Labs ReReplacer which could be set up to replace the .item-204 code with .item-204 plus any additional code you require. I am assuming .item-204 is unique on the page.
The advantage of using this extension is that you don't have to find or update the core code. Updating core code is often considered bad practice as any future template or extension updates can undo your changes.
It's really just about adding this code to every page? Instead of adding another plugin so your site you can put your code in a script tag located in your templates main php file. This file is named index.php and is located here: /templates/[your template name]/index.php
If you want to do it even better, place this code in the templates JS file. Some of them have a file like /templates/[your template name]/js/custom.jswhich is a good place to add custom JS to.

Joomla output override from plugin

Is there a way to override a component's output from a plugin? I'm aware of the concept of template overrides, but I just don't want to put the code into the template because
it simply feels wrong to put the code there because it is not a feature tied to the template. In my opinion the template should only contain overrides to fit core output into the template
I may want to add parameters to change behaviour (i.e. there is business logic involved and I want to keep it all together - in a plugin)
I may reuse it at another webpage and obviously don't want to copy & paste the code to all the templates I use
Or are my considerations wrong and the answer is: Just keep it simple and put it into the template?
I found a tutorial about Adding custom fields to core components using a plugin in the joomla documentation where the costom fields are addet to the view with a template override - which seems odd because the rest is done in a plugin - so i assume there is no way to to an output override from a plugin?

How do I get my child theme in wordpress to work with multiple style sheets?

I have styles.css, footer.php, header.php, and functions.php in my child theme folder. (wp-content/themes/kingdom-child) This site is composed of woocommerce and bootstrap files. I have this in my functions.php file :
<?php
function enqueue_child_theme_styles() {
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('kingdom-style') );
}
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
I have this in my styles.css file:
/*
Theme Name: Kingdom - Woocommerce Amazon Affiliates Theme
Theme URI: http//smarttvsandgadgets.com/
Description: Kingdom - Woocommerce Amazon Affiliates Child Theme
Author: Charlie Aubuchon
Template: kingdom
Version: 1.4
*/
/* =Theme customization starts here
----------------------------------------------------------------------------------*/
header.php and footer.php are untouched.
I tried making some styles to alter the theme and it didn't do anything.
What am I doing wrong? (not much knowledge of php, found all examples on the web.)
In Documentation some examples were for older WP. I have updated the minute stuffs. Theme Handbook has older reference, because people left project and same people actually continued to code for main WP development. Two years old story. Essentially WordPress.ORG is a voluntary project. There can be minute differences among docs. But OP had actually a basic question. It has been made complicated.
If OP needs only customization, learning enqueue is not needed for basic usage.
Preferably, keep a backup copy of the style.css. Edit in proper editor like vi or gedit for the sake of syntax highlighting. Open style.css in text editor. Do not use WP's function. If you edit in MS WordPad, I do not what can happen.
If modification does not work, it can mean many things like the Cached page is loading, user agent is respecting some other directive, OS + Browser combination is bad and so on. Use a developer tool to debug on Chrome or Firefox with empty cache, cookies. Using the WP Editor can fail for browser caching. Cache plugins should be off. Edit on a dev server. Ensure these points first. Then remove this , PHP_INT_MAX part and try.
How bootstrap is compiled that can matter.
More for the OP
footer.php, header.php, and functions.php in my child theme folder.
Yes, never touch that functions.php file without backup. Whenever we'll say you to add this snippet to functions.php, unless otherwise mentioned, it is that file. Without it, child theme will not work. footer.php and header.php are not mandatory files. We usually override default functions with these files. If home.php or front-page.php is present, that will override your settings from WordPress Admin for homepage.
I've nothing to do with 00M or that paid theme. You at this level need not to know about PHP. May be other stylesheets are loading - check the resources with webpagetest.org. It is likely that you are editing a file that actually is not what you need to edit. Use developer tool. Without touching PHP (except removing that , PHP_INT_MAX - note the comma, it should be removed too) you can customize.
Other unnecessary topics came via the answer
Without the comments, this is exactly the right for newest version of WP :
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
}
... other stuffs ...
Result will look like importing CSS.
I have no idea about M00. I guess OP added <?php opening here, it has more stuffs above the lines. It is normal to have some comments on the file's starting.
This will be right way via a Plugin like Header and Footer Plugin, as pure HTML :
<link rel="stylesheet" type="text/css" href="https://42.akamai.net/stackoverflow.com/questions/d.css" media="all" />
<!-- media="value", value = all, print, ...
Old reference from w3c
http://www.w3.org/TR/html401/struct/links.html#adef-rel
Microsoft Windozzz IE [1 to 8] can fail to fetch huge number of resources.
Conditional IE specific CSS might be needed.
-->
We often deregister materials and serve in this consistent way. Reverse functions. For own website we need the absolute URL, for developmental purpose the PHP functions are used. These are for additional stylesheets not for basic customization but optimization.
PHP_INT_MAX in add_action() points to the fact that the developer wanted the parent stylesheet to appear after all the other stylesheets has been loaded. OP can remove it to test. It is not possible to say whether the usage is wrong unless we can see the whole functions.php.
There are number of ways to print the path or uri because we do not know what is the website's FQDN :
get_template_directory_uri()
get_stylesheet_directory_uri()
get_stylesheet_uri()
get_theme_root_uri()
get_theme_root()
get_theme_roots()
get_stylesheet_directory()
get_template_directory()
I have not understood why suddenly the person referenced WP enqueue script function and wrongly cited my webpage and Net Tut+ guide. They are for different purposes. Not for child theme's stylesheet customization. StackoverFlow has a value. I, at least never say to do a wrong cocktail of PHP and HTML! One can have different opinion but I never said to not use WP enqueue script for the child theme's main stylesheet. If you have any question, before putting the link, you could ask us. That was for non-blocking Java scripts and have async, defer. I respect your learning and helping, but possibly you should review your answer and correct the syntax.
We were talking about other things, that person has misunderstood. Here is child theme's reference :
codex.wordpress.org/Child_Themes
Again, I have nothing to do with OOM or that child theme. I came here by searching domain name to disavow. I gave the links and the solution. Thats how people starts using WordPress. After 5 years, if you continue tweak, you'll become micro-guru for maintaining your own websites.
Sorry for the bigger reply.
According to your question, Two choices (my answer is no good)
If (for beginners only need stylesheet to work only as the fastest way)
Say you just want stylesheet to work only, why not use normal external css include way. and view source see if your css link is working and in the right loading position (is found instead of 404).
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css">
// previsouly got confused about the following 4 wp functions:
get_stylesheet_directory(): file path to current Theme directory
get_stylesheet_directory_uri(): url path to current Theme directory
get_template_directory(): file path to parent Theme directory
get_template_directory_uri(): url path to parent Theme directory
I personally do not follow wordpress template's default function way to add css, js. sometimes i do not care much about the benefit of enqueue (I admit it's wrong): https://wordpress.stackexchange.com/questions/49098/what-are-the-benefits-of-using-wp-enqueue-script
But I might just put it anywhere that is best for me to read or to Do overriding,control load order etc , in testing phase as my personal preference.
else, ( there are many benefits using enqueue scripts)
you take a bit more time debug it in understanding how the template load script and thier sequence, neglect the above hard-code script answer.
TWO more reference link on enqueue script;
The ins and outs of the enqueue script for wordpress
ttp://code.tutsplus.com/articles/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and-plugins--wp-22509
The advantages and disadvantages wp-enqueue-script function
ttps://thecustomizewindows.com/2014/04/advantages-disadvantages-wp-enqueue-script-function/
As I'm also low reputation user as the author,I would like to lend a helping hand using the limited knowledge i've researched and learned. while we are participating the discussion , providing related answers/references to such question, there are always many ppl who prefers to downvote to remove answers while there is actually no better answer comming yet or correcting the information refered. Such deed is not a good culture of discussion.

Joomla: including php-files renders jdoc-tags unusable

I'm trying to set up a Joomla-template in a way which allows me to use template parameters to choose between a number of layout variations of the template from the admin-interface. The idea is that the index.php only contains a statement which includes a php-file from a folder within the main template folder, based on the template parameters passed.
Unfortunately it seems thatmy jdoc module positions breaks whenever they are located outside the index.php file, even though it is referenced by an include-tag.
The problem is in the order that the framework builds index.php. Rather than trying to use include files, you can accomplish the same goal in a much easier and cleaner fashion. Using a combination of collapsible module positions and CSS you can manipulate the template to do basically anything you want.
You can also use template styles if you are using J1.7. A template style is basically a variation of a template that is assigned to specific menu items. Either way will work.

Wordpress and my own custom PHP stuff

Often times, when I try to add my own custom PHP source codes to the template files (sidebar.php, index.php, header.php, etc...), it generates errors. However, it works properly on it's own outside of Wordpress. Is there a way for me to use my custom PHP stuff in Wordpress' template files?
If you're talking about the sidebar.php, index.php and header.php in wp-content/themes/themename/, well, of course you can edit these files. They are meant to be edited. Only make sure that you don't overwrite existing PHP functions...
You can read about that on Wordpress' Docs
No plugin required to add your own php, but to maintain upgradability you should, as far as possible, avoid altering core WP files and place your code within your own theme's files or in your own plugins. If you're getting errors it's hard to guess at what they may be without details, but I've found that, apart from simple parse errors etc., the most likely causes are scoping errors.
With Wordpress you can extend Wordpress functionality by writing your own plugins. This is a fairly pain free process.
http://codex.wordpress.org/Writing_a_Plugin
You can also extend the functionality that templates have by putting functions into the functions.php template file.
http://codex.wordpress.org/Theme_Development#Theme_Functions_File
Adding custom php code to templates shouldn't generate errors. The only problem you might be having is that your code is conflicting with existing wordpress function names and variables.

Categories