Wordpress improve REST API - SHORTINIT not work - wordpress

i'm trying to improve the performance of Wordpress API custom endpoints.
I created a plugin, simple file under plugin/PLUGIN_NAME folder where I call "register_rest_route" function to set the endpoints.
To improve the performance I am trying to load not all the plugins, but only what I need, Wordpress CORE to query users and posts and Ultimate Members.
That's my code:
define('SHORTINIT', true);
require_once dirname(__FILE__) . '/../../../wp-load.php';
require_once dirname(__FILE__) . '/../ultimate-member/ultimate-member.php';
add_action('rest_api_init', function () {
register_rest_route( 'my-api/v1', 'test/me',array(
'methods' => 'POST',
'callback' => 'test'
}
));
...
...
It work, but the problem is that works also if I not load "wp-load.php" script. In my test method I use WP_User_Query, WP_Query and ultimate member method like um_user().
It seems like SHORTINIT didn't work.
What I wrong ?

Reading the source code of wp-settings.php shows a problem:
// lines 144 to 147 of wp-settings.php
// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT ) {
return false;
}
// lines 359 to 373 of wp-settings.php
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
/**
* Fires once a single activated plugin has loaded.
*
* #since 5.1.0
*
* #param string $plugin Full path to the plugin's main file.
*/
do_action( 'plugin_loaded', $plugin );
}
unset( $plugin );
The check for SHORTINIT is done before plugins are loaded. So your "define('SHORTINIT', true);" is executed after not before SHORTINIT is checked and has no effect.
Further wp-settings.php is included indirectly from wp-load.php so when your plugin code is executed wp-load.php has already been included.

Who have my same problem, I recommend using Plugin Load Filter, a wordpress plugin that let you select which plugins to activate in the REST API.

Related

Register Stylesheet if a particular plugin is installed

I use a basic custom plugin I have built for various WordPress admin functions.
One of them adds a custom CSS file that hooks into the admin as per the example below:
// Sets A Custom Admin Colour Scheme
function hits_admin_colour_scheme() {
wp_register_style('hits_admin_colour_scheme', plugins_url('/assets/css/admin.css',__FILE__ ));
wp_enqueue_style('hits_admin_colour_scheme');
}
add_action( 'admin_init','hits_admin_colour_scheme');
Is it possible to have an additional stylesheet that is only loaded if a particular plugin is installed? Say WooCommerce or ACF? This way I can reduce the size of the default CSS file and only load what is relevant.
Sorry if this is a newbie question. I appreciate any help.
You can check whether woocommerce(or others by same way) is activated before enqueue the file
function hits_admin_colour_scheme() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
wp_register_style('hits_aditional_colour_scheme', plugins_url('/assets/css/additional.css',__FILE__ ));
wp_enqueue_style('hits_aditional_colour_scheme');
}
}
add_action( 'admin_enqueue_scripts', 'hits_admin_colour_scheme');
and please use admin_enqueue_scripts hook to enqueue admin assets.

Can I create custom post type just for documents like PDF, Video, images? WordPress

What I Have done until now, I am creating a WordPress plugin to add menu page for the document management in WordPress
<?php
/**
*
*
*
* #wordpress-plugin
* Plugin Name: doc management
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/** Step 2 (from text above). */
add_action( 'admin_menu', 'my_plugin_menu' );
/** Step 1. */
function my_plugin_menu() {
add_menu_page( 'My Plugin Options', 'docs Management', 'manage_options', 'my_unique_identifier2', 'my_plugin_options','',4 );
}
/** Step 3. */
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
$file = plugin_dir_path( __FILE__ ) . 'view/main.php';
include_once $file;
}
?>
but I need to add functionality just like in WordPress Post and pages menu. So I am thinking instead of creating a new plugin for the functionality can I do it with custom post type. instead of adding pages or post I just want to add a document. that it's nothing else means instead of adding a new page. add a new document and list all those documents. bulk delete and all.
Is there any specific need that you are trying to build a custom plugin?
I recommend, you may try using https://wordpress.org/plugins/custom-post-type-ui/

Use my custom CSS just for my WordPress plugin settings page, how?

I'm developing my first WordPress plugin. I need to use a custom CSS for the settings pages that I created for the plugin but when I enqueue my stylesheet file, it affects the whole WordPress backend and not just my plugin's settings page.
Is possible to solve this problem? How?
When you want to add styles or scripts in WordPress you enqueue them using hooks. For the admin side the hook you are looking for is called admin_enqueue_scripts.
You can add something like
/**
* Register and enqueue a custom stylesheet in the WordPress admin.
*/
function wpdocs_enqueue_custom_admin_style() {
wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'css/woo-solo-api-admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );
You just need to be careful to correctly specify the url of the css script you want to enqueue.
Hope this helps.
Oh and https://developer.wordpress.org page is great for finding out about WordPress functionality, core functions, hooks etc.
Also check out the plugin handbook: https://developer.wordpress.org/plugins/
Loads of useful information can be found there :)
EDIT:
admin_enqueue_scripts has a parameter you can use read more
/**
* Register and enqueue a custom stylesheet in the WordPress admin.
*/
function wpdocs_enqueue_custom_admin_style( $hook ) {
if ( $hook === 'edit.php' ) {
wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'css/woo-solo-api-admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
}
add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );
This will load the script only on the edit post screen.
You can see what hook is loaded on your screen by adding
error_log( print_r( $hook, true ) );
In your wpdocs_enqueue_custom_admin_style function before the condition. Enable the debug log in your wp-config.php and you'll get a name of your custom post screen hook.
Alternatively, you could target the $current_screen to match the CPT's screen. This will load the script only on that page.

Automatically execute plugin in wordpress without activating them?

Whenever we use any plugin in wordpress , we need to go to plugin option and then we have to activate them to use them , its fine !
Now my question is
what if someone wants to execute the plugin by default without activating them ?
It means just install that plugin and that plugin will automatically execute on our site without any activation.
Thanks Mubeen for your answer but i just found another solution which is very simple and easy to understand !
Just create a folder name
mu-plugins
folder directory should be
/wp-content/mu-plugins
just download any plugin from www.wordpress.com and extract them and simply copied them in this folder , you will see a new tab in your wordpress plugins option as
Must-Use
the plugins under this tab will automatically executed on your site but there is a problem that if you want to deactivate that plugin then you have to delete that plugin from mu-plugins folder.
source:
http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users
You can use this code for auto activation WordPress plugin, this will help you to solve your auto activation plugin problem.
<?php
// example on admin init, control about register_activation_hook()
add_action( 'admin_init', 'your_activate_plugins_function' );
// the exmple function
function your_activate_plugins_function() {
if ( ! current_user_can('activate_plugins') )
wp_die(__('You do not have sufficient permissions to activate plugins for this site.'));
$plugins = FALSE;
$plugins = get_option('active_plugins'); // get active plugins
if ( $plugins ){
// plugins to active
$pugins_to_active = array(
'hello.php', // Hello Dolly
'adminimize/adminimize.php', // Adminimize
'akismet/akismet.php', // Akismet
'find-any-think/create-plugin-index.php' // Find any think Plugin
);
foreach ( $pugins_to_active as $plugin ) {
if ( ! in_array( $plugin, $plugins ) ) {
array_push( $plugins, $plugin );
update_option( 'active_plugins', $plugins );
}
}
} // end if $plugins
}
?>
Thanks, I Hope your problem will be solve by this code.

How can I disable WordPress plugin updates?

I've found a great plugin for WordPress under GPLv2 license and made a lot of changes in source code, plugin does something else now.
I modified author (with original plugin author's credits), URL, version number (from xxx 1.5 to YYY 1.0).
Everything works great, but when WordPress checks for plugin updates it treats my plugin YYY 1.0 as xxx 1.0 and displays notification about available update.
My changed plugin YYY 1.0 was installed by copying files from my computer, not from WP repository.
What else do I have to change?
Disable plugin update
Add this code in your plugin root file.
add_filter('site_transient_update_plugins', 'remove_update_notification');
function remove_update_notification($value) {
unset($value->response[ plugin_basename(__FILE__) ]);
return $value;
}
Put this code in the theme functions.php file. This is working for me and I'm using it. Also this is for specific plugin. Here you need to change plugin main file url to match to that of your plugin.
function my_filter_plugin_updates( $value ) {
if( isset( $value->response['facebook-comments-plugin/facebook-comments.php'] ) ) {
unset( $value->response['facebook-comments-plugin/facebook-comments.php'] );
}
return $value;
}
add_filter( 'site_transient_update_plugins', 'my_filter_plugin_updates' );
Here:
"facebook-comments-plugin" => facebook comments plugin folder name
"facebook-comments.php" => plugin main file.this may be different like index.php
Hope this would be help.
The simplest and effective way is to change the version of the plugin which you don't want to get update.
For an example
if I don't want wptouch to get updated, I open it's defination file, which is like:
/*
Plugin Name: WPtouch Mobile Plugin
Plugin URI: http://www.wptouch.com/
Version: 4.0.4
*/
Here in the Version change 4.0.4 to 9999
like:
/*
Plugin Name: WPtouch Mobile Plugin
Plugin URI: http://www.wptouch.com/
Version: 9999
*/
In the plugin file, there will be a function that will check for updates. The original author could have named this anything, so you will have to go through the code and check each function and what it does. I would imagine the function will be quite obvious as to what it does.
Alternatively you can add this to your plugin file:
add_filter( 'http_request_args', 'dm_prevent_update_check', 10, 2 );
function dm_prevent_update_check( $r, $url ) {
if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
$my_plugin = plugin_basename( __FILE__ );
$plugins = unserialize( $r['body']['plugins'] );
unset( $plugins->plugins[$my_plugin] );
unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
$r['body']['plugins'] = serialize( $plugins );
}
return $r;
}
Credits: http://developersmind.com/2010/06/12/preventing-wordpress-from-checking-for-updates-for-a-plugin/
add_filter('site_transient_update_plugins', '__return_false');
in function.php add above code and disable all plugins updates
Add this line to wp-config.php to disable plugin updates:
define('DISALLOW_FILE_MODS',true);
One easy solution was to change the version of plugin in plugin file.
For example if plugin version is 1.2.1. You can make it like below (100.9.5 something that plugin author will never reach to )
<?php
/*
* Plugin Name: Your Plugin Name
* Description: Plugin description.
* Version: 100.9.5
*/
Here's an updated version of Mark Jaquith's script:
WP Updates have switched to HTTPS
Unserialize was blocked on my shared hosting
This uses json_decode and json_encode instead
Credit: Block Plugin Update
.
add_filter( 'http_request_args', 'widget_disable_update', 10, 2 );
function widget_disable_update( $r, $url ) {
if ( 0 === strpos( $url, 'https://api.wordpress.org/plugins/update-check/' ) ) {
$my_plugin = plugin_basename( __FILE__ );
$plugins = json_decode( $r['body']['plugins'], true );
unset( $plugins['plugins'][$my_plugin] );
unset( $plugins['active'][array_search( $my_plugin, $plugins['active'] )] );
$r['body']['plugins'] = json_encode( $plugins );
}
return $r;
}
Disable plugin updates manually:
Open functions.php file (go to your activated themes folder)
Copy and paste the following code:
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );
Save changes, and you’re done
Just for completeness, here is one more plugin meant to block updates of selected other plugins:
https://github.com/daggerhart/lock-plugins
Some information about its background and mode of function can be found here (in German).

Resources