Wordpress and my own custom PHP stuff - php

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.

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.

Using include() instead of get_footer()

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

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?

Prestashop PHP Include in a .tpl does not work

I am creating a storefront for a client using Prestashop. Prestashop uses Smarty .TPL files. I read through smarty documentation and scoured the web but all of the suggestions are not working.
I first made a site using regular .php pages and I am including the header.php on every page.
I then made a directory for prestashop and got it set up. I edited the header.tpl file and was able to hard code in the header.php code. The problem with this is; when I want to edit the header (nav bar, images, social media), I will have to edit it in two different places. So I tried to "Include" my header.php file.
Though, when I try using smarty's {include_PHP "file.php"} and/or {PHP}include...{PHP}, Prestashop errors out and gives me a blank white page - no errors are given - (in chrome it gives me a "server error") until I take out the includes.
I tried replacing the entire header.tpl code with a smarty include and another piece of code that had a header hook, but none of these worked. Any suggestions? I just want one header where I only have to edit it once to make changes.
Using Prestashop v 1.4.4.0
Edit: I changed allow_php to true from false. Now it's trying to add the file, though it says it can't find the file. I placed it next to header.tpl and just used:
{php}
include('navBar.php');
{/php}
ANSWERED!
When using Smarty .TPL files, when you include something, you are not including from the path of the file you are working on. You are including from where the index is.
Example:
I am working on header.tpl, this is located in:
siteroot/smartyinstall/themes/themename/header.tpl
When the include is looking for the file, it is actually looking for it in the smarty root folder because the header.tpl is being pulled into the index.html page that is in the smartyinstall folder.
So, you have to go from there. In my case, the header that I was trying to include was in:
siteroot/includes/navBar.php
so, I had to write include('../includes/navBar.php');, only going up one directory, instead of four.
I hope this helps everyone that has a problem like this!
It's considered very bad practice to include php in smarty .tpl files so I would strongly recommend you don't add code this way. One of the main reasons for disabling the {php} tag is to help prevent code injection attacks. eCommerce sites are by their very nature natural targets for exploits.
A better approach would be to override the FrontController class to assign your custom code to a smarty variable - this could then be inserted into the header.tpl without resorting to using a php include().
With the class and controller overrides available in Prestashop 1.4.x there's no real reason you should need to resort to hacks and/or modifications to the core distribution.
Paul

How to use two Wordpress templates at once... or something similar?

I'm building a wordpress template with a lot of javascript, so I am also setting up a fallback version what requires different php in the index file of the template, and preferably, different header files.
The best way to do a fallback seems to be setting a javascript redirect on the "non-javascript site" to go to the "javascript version" of the site.
I am a bit confused on how the index.php file from the template folder trickles all the way down to the root directly. Yes, a understand php includes, but don't know how to jam a different file down the wordpress pipeline.
I was thinking I could do a redirect to mywebsite.com/index2.php but then I realized I didn't know how to get that to work.
Any ideas on how to do this?
It isn't the index.php that handles what template you use. Is a field in the database.
You would need to create a new theme that has both and switches them based on a session variable.
I am guessing this is nontrivial.

Categories