Overriding plugin functions from a theme - wordpress

There is a plugin that has all of its presentation functions declared so that they can be overriden. I.e.:
if (!function_exists('show_thing')){ ... }
How should this be done? I tried declaring the function in my themes functions.php but it was already declared by that point of execution.
If I create my own plugin with the method declarations then how can I ensure my plugin loads before this plugin does and functions are definitely overriden?

The easiest way to do this would be to use a Must Use Plugin. Create a folder called /wp-content/mu-plugins. Any PHP file in this directory will be loaded (they do not have to be activated) and will be executed before the activated plugins or your theme files (including functions.php). Use a file here to define the functions that you want to override from the plugin.
Check out this diagram for more info on the WP Core load order: https://wordpress.stackexchange.com/a/26622/20880

Related

WordPress performance - loading backend scripts when logged in in frontend

I use WordPress with the plugin "UserWP" to create a member area for logged in users. Everything is working and looking great but when i check the code i can see that on my pages (fronted) its loading tons of js and css that is used for the WordPress backend.
Example:
/wp-includes/js/media-views.min.js?ver=6.0.1
/wp-includes/js/dist/hooks.min.js?ver=c6d64f2cb8f5c6bb49caca37f8828ce3
And tons of other small scripts.
Is it possible to disable this for the pages i created and i use my frontend-template?
CSS & JS files from /wp-includes/ directory shouldn't be removed.
However, you can remove plugins and theme useless files using wp_dequeue_style or wp_dequeue_script functions.
These functions should be called inside your custom function and that custom plugin should be hooked with wp_enqueue_scripts action.

Block.json editorScript path references plugin directory

I am using block.json in a WordPress theme for each block withhin but the editorScript is referencing the plugins directory and not the theme.
For themes, you should use theme.json, the newly embraced practice for defining properties for the block editor. Part of theme.json provides for individual blocks and the contents are what you are using for block.json. I think block.json should be reserved for use in plugins where it defines the settings of individual blocks. Use theme.json for defining global settings, including blocks, if contained in the theme.
This behaviour was reported as a bug and will be fixed in Wordpress version 6.0.
Github pull request:
https://github.com/WordPress/wordpress-develop/pull/2494

Can we defer the loading of MU-Plugins in a Wordpress installation

Im using the WordPress Multisite MU-Plugins directory to run some custom functions which i need to be
available to ALL subsites in my network. When i run the function in the primary sites' functions.php
file it runs as expected but it obviously cannot access any subsites.
Is the problem with the loading order that WP utilises - i read here the order is:
the WordPress core code
mu-plugins
plugins
functions.php
the theme code for the specific template being displayed
I believe the issue may be that im trying to call wp funcs that are not yet loaded yet. Is there a way to defer the loading of 'Must-Use' -plugins until, say when functions.php runs??
You can't defer the load, no, but you can move the code in the mu-plugin that requires on other things to be loaded into action hooks for actions that are triggered later in the load order:
hook plugins_loaded to run code after all plugins have loaded
hook after_setup_theme to run code after the theme has loaded
(I guess that's what you mean by 'functions.php' ?)
However I think all of WordPress's own functions are loaded before mu-plugins are loaded, so this may not be your problem.

How to use selected WP function in custom PHP file?

I have a custom PHP file on which I want to use some WordPress functions such as wp_get_current_user(). I've tried requiring wp-load.php, but that increases the load time significantly as it loads all WP functions.
Is there anyway I can use only the functions I need from WP?
Thanks, Emir
You can create page template or plugin then you able to access wp_get_currunt_user() function in you template file or plugin file

Files ti include in wordpress so that defined terms works?

What are the files needed to include in wordpress so that all the defined terms and function works in user created folder inside the wordpress theme folder?
Note: I have created a folder inside wp theme folder and I want to use wp_insert_post()function.
You should Include the wp-load.php in external file to access the Wordpress functions.
Note: There may be better solution for you instead of creating external file inside theme.

Resources