Wordpress - Get root directory? - wordpress

How can I get the root directory of my site? I want to use it as the src for <img>.
For example If I install the wordpress on www.mysite.com/blog/, the root is /blog.
But If I install it to www.mysite.com/test/blog, the root is /test/blog.
I tried to use the snippet from this question, but it returns the whole path as seen in FTP. So instead of returning just /blog, it returns /home/public_html/blog which won't work if used as src in image tag.
Any solution? Thanks.

You may use site_url() (eventually with echo) to get absolute path with server name and directory. Also, have a look at wordpress documentation about similar functions & what they provide.

You may have better luck over at the Wordpress Stack Exchange site :)
And this suggestion to use ABSPATH didn't help on that thread? https://stackoverflow.com/a/2356467/413254

site_url() return the path with http, in some cases, it is not allowed if the server turns off url inclusion. You can use the function below to get the root path of wordpress installation folder:
function get_wp_installation()
{
$full_path = getcwd();
$ar = explode("wp-", $full_path);
return $ar[0];
}

Related

How can I integrate wordpress into magento 2 project in valet plus?

I am using Valet+ for my Magento projects.
Currently I’m trying to add WP integration for M2 project. I put wordpress copy into pub/wp/ directory and want to go through the installation process, but I’m receiving 404 error when I’m trying to access url like https://magento2.test/wp/
Could anyone advice for it?
Thank you!
You can use custom valet driver for it.
Copy Magento2ValetDriver.php into magento root directory and rename it to LocalValetDriver.php
Also, change the class name to LocalValetDriver in it.
In the function frontControllerPath, add the following code at the top.
if ($uri === '/wp') {
$_SERVER['DOCUMENT_ROOT'] = $sitePath . '/pub/wp';
return $sitePath . '/pub/wp/index.php';
}

Get WordPress plugin directory URI

I want to include a WooCommerce file while running a plugin. Here is the code I am using,
include_once(include( content_url() .'/plugins/woocommerce/includes/wc-core-functions.php'));
This gives me below error,
include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /nas/content/staging/squatwolf/wp-content/plugins/wpfront-notification-bar/classes/class-wpfront-notification-bar.php on line 58
I understand its being caused because the content_url function is giving complete url instead of relative url. I also tried,
plugin_dir_url(__FILE__)
but it gives the url of the plugin I am in, not the wordpress plugin directory url.
you want like this : /home/user/var/www/wordpress/wp-content/plugins/?
try $dir = plugin_dir_path( __DIR__ );

Wordpress plugin, do not prepend host in wp_register_script

I am pretty new to WP and have a little question about the wp_register_script function,.
I have a dev environment where the url is test.example.com .. right know when i make a plugin and use wordpress wp_register_script to insert the .js and .css files wordpress prepend the host in front of the url ..
Is there a good way to handle this?? without creating a test DB and change the "siteurl" ?
I found a workaround for my issue ..
I could set WP_SITEURL in the we-config.php file (not the best solution)
if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == "test.example.com") {
define('WP_SITEURL','http://test.example.com'); // use this if we are testing on test.skala.fm (not localhost)
define('ENVIRONMENT', 'local'); // using localdb
//define('ENVIRONMENT', 'production'); // use this if we are testing on test.example.com (not localhost)
} else {
define('ENVIRONMENT', 'production');
}

How do i locate wordpress plugin directory?

I am trying to add a function from plugin 1(wp job manager) to plugin 2(woocommerce).
I have decided to do this by including the php file from plugin 1, however I am unable to locate the file directory. I have used:
include( plugin_dir_path( __FILE__ ) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');
but it returns the following error:
Warning:
include(/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/wp-job-manager/includes/class-wp-job-manager-applications.php): failed to open stream: No such file or directory in
/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/functions.php
on line 77
Please advise me as I've been stuck on this issue for really long... Thanks!!!
Wordpress setups have a constant ABSPATH defined (look at the bottom lines of wp_config.php) which points to the full and absolute path of the Wordpress setup, so in your case echo ABSPATH; would return /home/content/p3pnexwpnas05_data02/78/2394078/html/.
For most installations, appending wp-content/plugins/ to that string would point you to your plugins directory.
However, in a Wordpress configuration one can also customize the wp-content and or plugins directory to their own preference, so building plugins on ABSPATH.'wp-content/plugins/ is not recommended. Unfortunately Wordpress simply doesn't have a get_absolute_pluginspath() function or something available. A trick would be to fetch the plugins-URL, and remove the site-URL from it, so the remaining data is wp-content/plugins/ (or whatever the user has made of it). In code:
$plugins_directory = ABSPATH.str_replace(site_url()."/","",plugins_url())."/";
Which in your case would return:
/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/plugins/
You probably mean:
plugin_dir_path(__FILE__)
That gives you the directory path to the file that statement is in. So what that returns depends on where you run it. If you use this statement
include( plugin_dir_path(__FILE__) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');
in the main plugin file for wp_job_manager (probably wp_job_manager.php), then plugin_dir_path(__FILE__) give the path of the directory that file is in (the plugin directory).
If you use it in some other file, you will need to adjust the rest of the path string accordingly.

How to run codeigniter(HMVC) inside wordpress folder

Anybody have install codeigniter(HMVC) within wordpress.
I am trying to install codeigniter(HMVC) inside wordpress folder.
my codeigniter is work on Virtual host can it possible to run codeigniter within wordpress setup with codeigniter is work on virtual host.
Any suggestion.
It is very easy. Here is step by step process.
First see the structure.
You can see it is inside wordpress directory. Next Add this in index.php of codeigniter
require_once('../wp-load.php');
And finally to avoid conflict between Wordpress and Codeigntier's site_url function add this file in codeigniter/application/core as MY_url_helper.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('ci_site_url')) {
function ci_site_url($uri = '')
{
$CI =& get_instance();
return $CI->config->site_url($uri);
}
}
if (!function_exists('ci_base_url')) {
function ci_base_url($uri = '')
{
$CI =& get_instance();
return $CI->config->base_url($uri);
}
}
Now when you use site_url() it will refer to wordpress site_url() and
ci_site_url() it will refer to codeigniter's site_url.
And now when you need to access codeigniter try this.
`http://localhost/wordpress/codeigniter/index.php/mycontroller/mymethod`
Hope it is easy to set up and helps.
Codeignetor can run OK in every directory if server allows to install on that directory.
If its not working please confirm the security settings from server.

Resources