Access own defined variable from wp-config-php - php

I am trying to define my own variable in wp-config with below code:
/* Add any custom values between this line and the "stop editing" line. */
define( 'MY_VARIABLE', 'MY_VALUE' );
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
I then try to echo the variables like this in a php file in my theme:
echo MY_VARIABLE;
echo ABSPATH;
Which gives the following output:
MY_VARIABLE (wrong, expected MY_VALUE)
path/to/..... (expected)
Why does it echo the variable name instead of the variable value? But for ABSPATH it works as it should?

Related

Integration testing of a WordPress plugin that needs composer and npm to be executed

I am writing integration tests for a plugin, and I did everything using wp-cli and scaffolding the tests. And they are running fine when I run phpunit. But the problem I have is that I am using composer and npm - composer for some extra functionality, and npm for bundling my scripts.
The scripts part is important because I'm enqueueing the scripts from the public folder (build folder)
$main_script = 'public/scripts/application.js';
wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );
wp_enqueue_script( 'plugin-scripts' );
I need to test if my scripts and styles are enqueued, so I added a test
public function test_enqueued_scripts() {
$this->admin->enqueue_styles();
$this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}
$this->admin is just an instance of the class where my enqueue method is in the setUp() method.
And I get an error because it says that Failed asserting that false is true.
The plugin I'm testing is built and composer is installed. All the folders exist and everything works when I'm locally on my WordPress instance. But the test instance is not the same as my local instance (ofc). I error_loged in the enqueue method to check if the file_exist and I get false.
I need to test this with phpunit (clients requirements are to have full test coverage).
My bootstrap.php looks like this
<?php
/**
* PHPUnit bootstrap file
*
* #package Plugin
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
exit( 1 );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
// Update array with plugins to include ...
$plugins_to_active = array(
'advanced-custom-fields-pro/acf.php',
'my-plugin/my-plugin.php',
);
update_option( 'active_plugins', $plugins_to_active );
require dirname( dirname( dirname( __FILE__ ) ) ) . '/advanced-custom-fields-pro/acf.php';
require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
How can I start the build process (npm run build) so that my scripts exist before my unit tests? Also is it possible to have this build step run only once, not once every time I run phpunit?
A very basic solution would be to check for a file that npm creates with file_exists() in your bootstrap.
If it aint there, run the build with shell_exec()
http://php.net/manual/en/function.file-exists.php
http://php.net/manual/en/function.shell-exec.php
You need to run do_action('wp_enqueue_scripts') prior to executing your assertions. This article has more details and an example. So, your test should look like this:
public function test_enqueued_scripts() {
$this->admin->enqueue_styles();
do_action('wp_enqueue_scripts');
$this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}
I've tested this solution and it works.

Loading an image in custom WordPress plugin

I created my custom plugin and want to add custom logo, but somehow it keeps redirecting me to wrong dir, this is error I get:
GET http://localhost/logo.png 404 (Not Found)
Here is what I tried so far:
$plugin_dir = str_replace( $base_url, ABSPATH, $plugins_url );
$plugin_dir = plugins_url();
$plugin_dir = plugin_dir_path( __FILE__ );
$plugin_dir = WP_CONTENT_DIR . '/plugins';
$plugin_dir = plugins_url( '/', __FILE__ );
Here is my add action:
function kalbos_modifymenu() {
add_menu_page('Kalbos', //page title
'Kalbos', //menu title
'manage_options', //capabilities
'kalbos_list', //menu slug
'kalbos_list', //function
$plugin_dir . 'logo.png',
'5'
);
}
add_action('admin_menu','kalbos_modifymenu');
However icon is working when i move my logo to wp-admin/logo.png dir because thats where my logo path goes even if i set it go to my plugin folder
plugins_url will (my emphasis):
Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.
So, the following will point exactly to your plugin directory with an ending slash:
$plugin_dir = plugins_url( '/', __FILE__ );
// results in http://localhost/wp-content/plugins/YOUR_PLUGIN/
And to target the logo, use:
$plugin_dir . 'logo.png'
Another example, to make a shortcut to your images folder:
$plugin_img = plugins_url( '/images/', __FILE__ );
// results in http://localhost/wp-content/plugins/YOUR_PLUGIN/images/
Try to add an other slash to /plugins like this: /plugins/ and also close your quotes on the same line. At the moment you have it like 'plugins; but the correct is '/plugins';

I am not including any file in my theme then how wordpress functions are working

Something that I just can't understand is how the Wordpress functions are loaded in. When I am adding an action to a Wordpress hook like this.
<?php
function test() {
echo "Test";
}
add_action('wp_enqueue_scripts','test');
?>
This code works for what I want to do but where is this add_action function coming from. I know Wordpress is taking care of it somehow but I just don't understand how I am able to call it without actually including a file. I tried to include the file in another file that would be included before this file but then I would get a function that is not defined error. I just really want to know the logic behind this.
You will find this function initialized in wp-includes/plugin.php :
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
You can use the function wherever this file is included.
Wordpress has this documentation and more # https://developer.wordpress.org/reference/functions/add_action/
It's work like, the first file that runs on open WordPress site is index.php which required wp-blog-header.php
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
then wp-blog-header.php require wp-load.php and template-loader.php
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
here wp-load.php file require wp-config.php
if ( file_exists( ABSPATH . 'wp-config.php') ) {
/** The config file resides in ABSPATH */
require_once( ABSPATH . 'wp-config.php' );
}
and wp-config.php file require wp-settings.php
require_once(ABSPATH . 'wp-settings.php');
and wp-settings loads wp-includes/plugin.php file
define( 'WPINC', 'wp-includes' );
// Include files required for initialization.
require( ABSPATH . WPINC . '/load.php' );
require( ABSPATH . WPINC . '/default-constants.php' );
require_once( ABSPATH . WPINC . '/plugin.php' );
and wp-includes/plugin.php file have add_action function in it
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
and wp-includes/template-loader.php load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );

MAMP to Live Server :: Fatal Error Class 'wpdb' not found in load.php

I'm trying to move my site from MAMP to the live server and I'm getting this error:
Fatal error: Class 'wpdb' not found in /data/folder_name/public_html/wp-includes/load.php on line 404
I went through the basic steps of moving everything. Created the Database, Imported my database, updated the wp-config.php and uploaded all of my files so I'm not sure what the issue is. I'm not even sure where to start.
Here is the code in load.php
/**
* Load the database class file and instantiate the `$wpdb` global.
*
* #since 2.5.0
*
* #global wpdb $wpdb The WordPress database class.
*/
function require_wp_db() {
global $wpdb;
require_once( ABSPATH . WPINC . '/wp-db.php' );
if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
require_once( WP_CONTENT_DIR . '/db.php' );
if ( isset( $wpdb ) )
return;
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );<--Line 404
}
Looks like it was an issue with the files loaded and nothing else. :-(

Incorrect url returned by plugins_url

I'm trying to get a url to my plugin, but plugins_url function returns an incorrect one.
in main plugin file there's this line:
$this->plugin_url = plugins_url( '/', __FILE__ );
Note, I've changed my plugins folder through wp-config.php:
...
define( 'WP_PLUGIN_DIR', '/home/victor/hg/' );
define( 'WP_PLUGIN_URL', 'http://hg.victorpc.org' );
...
hg.victorpc.org is a vhost with document root set to /home/victor/hg
the function returns this URL http://hg.victorpc.org/home/victor/hg/<plugin-folder> and the correct is http://hg.victorpc.org/hg/<plugin-folder>
Use plugin_dir_url() instead.
Codex: http://codex.wordpress.org/Function_Reference/plugin_dir_url
Usage:
$this->plugin_url = plugin_dir_url( __FILE__ );
This will return: http://hg.victorpc.org/hg/<plugin-folder>/ (notice the trailing slash).
I've worked around it, using:
plugins_url( basename( __DIR__ ) );
which returns what I expect.

Categories