Include central function that applies for all my Wordpress themes - php

I have a Wordpress network with a big amount of different themes and even more pages using those themes.
Now, I have a PHP function (for some tracking) that I want to run every time a page from my network is accessed. I don't want to edit all my themes, so I am looking for a central file where I can have this new function.
First thing in my mind was the index.php in the root folder of the WP installation. This would probalby work but does not seem the finest solution.
All my themes are using the wp_head function maybe this can help somehow?
Is there any other place where I can add my tracking code and make sure that it is run on every page access?

You should create a plugin to do this. In that plugin you will have that function and add the action to the wp_head hook. This will keep it working with all of your themes and easy to change without having to go into all your themes.
The code will look something like this
/**
* Plugin Name: Tracking Function
* Description: Lets me track my code
*/
function tracking_function(){
//enter code here
}
add_action("wp_head","tracking_function");
Then, because you have a network of sites, you will have to go into the Network Plugin manager, and activate the plugin for all networks and then you can activate or deactivate it for each specific site in your network.

The two options I can think of would be to use a plugin that loads "custom functions" for the sites that it is included on - much like functions.php in your theme, but independent of each theme. I wrote a plugin that does this which you can find here: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-custom-functions-php/
The other option would be to put a PHP file with your code into the wp-content/mu-plugins (Must-Use Plugins) that will be loaded before all other plugins on every site in your network. A bit more straightforward than the first option, but you can't selectively disable it should you not want on it on one of your network sites.

Related

Adding Monarch plugin to stand alone PHP script emulating WordPress layout

I'm putting together a WordPress site for a charity organization which upgrades the look & feel of their existing site. Their existing site had a few PHP scripts and an events database for their project listings and I changed the output so that the script emulates the WordPress UI using:
define( 'WP_USE_THEMES', true );
require_once "wp-load.php";
The site uses the Monarch social plugin to enable sharing and I need to have that work on the emulated project page.
I haven't been able to find what I should include.
I tried adding both of these lines, separately, but they had no affect:
require_once "wp-content/plugins/monarch/monarch.php";
require_once "wp-content/plugins/monarch/core/init.php";
Upon looking at the code and trying to call functions that looked like potential functions to execute it, they resulted in an error or does nothing.
As someone completely unfamiliar with the intricacies of WordPress doing a pro bono favor for a charity organization, I did not realize that page templates could be used as the destination for custom PHP code accessing a non-WordPress database, etc.
The solution steps (which may hopefully help someone else equally unaware of WordPress) are as follows:
Use the page template page as reference to create a page template.
I added my page template to the same location as the theme's page.php template
Then create a page in WordPress and specify its template as the one just created.
Call that new page with whatever parameters after ?xyz=value;abc=etc. as needed
This may be basic for people who live in WordPress but it wasn't immediately clear to me.
Thanks to the comments by #ChrisHaas!

Wordpress Dev: Understanding Scope of plugins or general WP Architecture

I am trying to understand a bit about how WordPress works in development..
I already created some shortcodes that I can just edit in on my page and some WIdgets that seem to display on every site or blog post that I created...
My question is: What is the general scope of the widgets or plugins in WordPress that I create? I know there are hooks and WordPress API functions to only show them on specific parts of WordPress, e.g on the admin-panel, or just in the widget sidebar of a page...
But are there some good sources about the architecture and scopes of how and where to use your plugins? Are they kind of injected into every page I create on my WP pages? (e.g with enqueue_scripts or so?). It's just a bit too overwhelming for me as a beginner to get a good overview...
And how are those functions from WP API made accessible in my plugin files/folders when I never imported them? I guess it's based on some module technique but no idea how..
It's good to delve your feet into the WordPress (WP) new concept called blocks you can find all the details here: Block Documentation
Creating widgets and shortcodes is a way older process nowadays most people keep their eyes on WordPress Blocks and especially FSE (Full Site Editor) therefore I would suggest you start learning that thing ASAP.
Let's come to your queries:
How WordPress works technically
Go to your WordPress directory you will find the file called index.php which is responsible for bootstrapping your application
Along with the index.php you will find a few directories called
wp-admin
wp-includes
wp-content
wp-admin
Where WP have all functionalities happening in the admin part are executed by accessing classes and functions present inside of this directory
wp-includes
This directory has all the utility and helper classes and functions
wp-content
This is the only directory developer can add/edit the files which store all of your plugins, themes, uploads, and other custom directories if your
code or any other plugin code created
For more details on how WP works
It's quite a challenge to explain all the ins and outs WP in a single post but you can find the detailed explanation in the official documentation
WordPress mainly depends on hooks and hooks are come under any of these two categories:
action hook
filter hook
For more details on WP hooks
How do plugins or themes work?
Both plugins and themes have a terminology called headers using that header metadata WP fills all the details in the plugins listing admin page and in themes listing page
For more details on header
Themes mainly depend on the concept called loops which responsible for fetching the posts data and build the necessary details using template tags and iterate them using the loop. Based on the type of page the user visited WP uses the template hierarchy to render the page as per the request that happened on the client end.
For more details on how WP loops works
For more details on how template hierarchy works
Headless CMS
Yes, you can make your complete WP into a headless CMS with the help of WP REST API. The REST API is used to access WordPress outside of the WP for e.g. if your android app need to fetch any posts or categories or user from WP using this REST API it can do that. Also you can create your custom endpoints too
Additionally, try to explore WP CLI
I believe that I covered most of the things required to understand how WP works, to be clear this post abstracted many of the things to make it as compact as possible for anyone who started entering into the WP development.
If you like to know more of these abstract explanation in a detailed way then always visit the official documentation
If your ide have an auto-complete feature please try to explore all of these functions (wildcard list) which contains most of the security functions provided by WP
is_*
exists_*
validate_*
sanitize_*
esc_*
*kses*
*nonce*

Wp-less-cache issue changing CSS

I'm making a web with a Wordpress and modifying some things with a child and css theme. The problem is that the theme uses a folder with the name "Wp-less-Cache" and inside it introduces a "theme-less.css" that it generates automatically. The problem is that now when I try to modify something by css the my mytheme.css (in the child) does not leave me because it tells me that what I want to retouch belongs to the file "theme-less.css" .... and if I tweak the CSS directly in that, it works but when this file is generated again automatically after a while it deletes the modifications that I had made in it.
I would like to know:
Can you disable the wp-less-cache? it is not in my list of plugins or among the plugin folders ...
Can I do something so that my modifications of the child theme are effective if he is active?
Thank you very much
It is used by one plugin called tlg_framework which is a built in plugin with themes that developed by http://www.themelogi.com/

Wordpress plugin using existing theme

I am building a plugin for the first time and I have setup my rewrite rules to call a PHP file I have located in my plugin folder and this is working but all I have it output just now is "test"
I wanted to ask, is there is a way of pulling in the existing theme and using this as the basis for the page, then I can have my plugin just output the page content area so to speak.
My plugin is quite big in terms of the amount of data it handles so I would like to be able to use a menu link to the aforementioned file and this file can then output all the sub pages of content but still using the default theme (theme I have made) and fill the content area only is this possible and if so is there anything to explain this already available because I couldn't see it.
Any help with this would be much appreciated.
Edit: I have tried including a file from my current theme but this will give me a 500 error so I assume its not as simple as this.
Have you thought about using custom post types for your plugin content rather than relying on custom tables and separate code?
Other than that, you could use a shortcode (just one) and have users insert that into a regular WP page, the shortcode then displays all of your various plugin stuff.
It's hard to be more specific without understanding why you've done it this way.

WordPress Mobile Theme Switcher Without Plugin (User Agent and Cookie)

I found some plugins like WP Mobile Pack, but I want to put together my own very simple theme switcher. (Just don't want any bloat.)
I have a regular WordPress theme and I also created a WordPress Mobile theme as well. These are both standard WP themes that can be activated in the dashboard.
How can I direct certain user agents directly to the mobile theme? (I don't want a mobile domain, sub-domain, or trailing directory.) www.example.com should be the domain for any visitor.
The one catch is, I also want to offer a link to let users switch back. For instance, iOS devices should go to the Mobile Theme. But if they are on the large iPad, they may want to simply use the regular site. Having some sort of cookie override the default .htaccess or PHP redirection would be useful.
Any help, specifically targeted at WP and handling WP theme switching would be truly appreciated.
I would use 2 stylesheets to theme the page, and detect the browser and serve up the right css depending on what the browser is detected as. Have a look at the 'get_browser' function of PHP http://php.net/manual/en/function.get-browser.php
then just have an if statement:
if (preg_match('/mobile/i', $u_agent)) {
$css = 'mobile.css';
}
and then just load the $css
Hope that makes some sense.
You will have to create a plugin, that fires the switch theme action. Here is a reference:
https://codex.wordpress.org/Function_Reference/switch_theme
I think you can try the 'after_setup_theme' hook.

Categories