How to use selected WP function in custom PHP file? - wordpress

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

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.

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.

Transfer the code of a custom plugin into a theme

I am working on a new project on WordPress.
I am developing a new website for a client based on their actual site (developed by an other team).
So, I decided to create a new WordPress theme and to use the existing content (client's request).
However, this supposes to reuse the custom plugins, in order to not re upload the pictures and the articles by hand.
I would like to include the custom plugins into the new theme. However, the plugins folder is outside the theme folder. So I was wondering if you have an idea on how I could transfer the code of a custom plugin into the new theme.
Thank you in advance.
What custom plugins are being used to handle pictures and articles? Uploaded files should go to /wp-content/uploads and all data is stored in the database. Removing the plugins might make the data inaccessible, but it shouldn't disappear.
Why are you moving functions from plugins to a theme? Or are these plugins inside of the /wp-content/themes/ folder? The way you handle the two cases is very different.
If you need to copy certain functions into a theme, you can move them into a functions.php file (or a functions.php file that calls to other files in that directory) in a child theme. The WordPress Codex has much better documentation on child themes than I could ever outline here. Placing the functions into your main theme is a permanent change and is not recommended when you're dealing with functions that are better-suited to plugins.
If you're copying plugins from one theme into another, you might want to look at plugin dependencies using TGM Plugin Activation.

Overriding plugin functions from a theme

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

No access to wordpress functions from plugin files

I came across this problem when I was creating a plugin. I cannot call(access ) wordpress default functions like wp_get_current_user(), $wpdb->get_results(), admin_url() etc. from my plugin files. I can access them from my main file of that plugin in which plugin description is given. But I cannot access them from other plugin files. At that time I heard that if we create a new file functions.php and put these code in that file inside a function I can access all wordpress functions. I did so. But now I cannot access that new function. How can I solve this problem? I think you guys understood what I said. Please help me.
#Jan : I got the required thing. I just put require_once('../../../wp-load.php'); in my plugin files and now can access all wp functions from there. Any ways thanks for your reply.

Resources