Cant access wp-load.php dynamically - wordpress

How to have access to wp-load.php ?
I can access it using the following
require_once("../../../../wp-load.php");
But I need to find it dynamically so I am using the following but none of them works.
require_once( dirname(__FILE__).'/../../../wp-load.php');
require_once( dirname(__FILE__)."/../../../wp-load.php");
require_once( ABSPATH.'wp-load.php');
require_once( ABSPATH."wp-load.php");
How to have access to localhost:8888/wordpress/wp-load.php?

Add below code snippets at beginning of file where you require to load wp-load.php
/* FindWPConfig - searching for a root of wp */
function FindWPConfig($dirrectory){
global $confroot;
foreach(glob($dirrectory."/*") as $f){
if (basename($f) == 'wp-config.php' ){
$confroot = str_replace("\\", "/", dirname($f));
return true;
}
if (is_dir($f)){
$newdir = dirname(dirname($f));
}
}
if (isset($newdir) && $newdir != $dirrectory){
if (FindWPConfig($newdir)){
return false;
}
}
return false;
}
if (!isset($table_prefix)){
global $confroot;
FindWPConfig(dirname(dirname(__FILE__)));
include_once $confroot."/wp-config.php";
include_once $confroot."/wp-load.php";
}

The same question is here.
Denzel Chia answer was :
Put the following in the beginning of your file that requires
wp-load.php
//include wp-config or wp-load.php
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
// WP 2.6
require_once($root.'/wp-load.php');
} else {
// Before 2.6
require_once($root.'/wp-config.php');
}
Hope it helps

Related

Allow contributors to upload images but not delete them

I enabled image uploads for contributors on my Wordpress site via the following code:
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
It works great, but I also need to disallow them to delete the images they uploaded. Is there an easy way to do it? I know there's User Role Editor plugin, but I don't want to install it just for this.
You can use delete_attachment hook or pre_delete_attachment filter for that:
add_action('delete_attachment', 'wp66410923_prevent_delete_attachment', 11, 1);
function wp66410923_prevent_delete_attachment($postID)
{
if (current_user_can('contributor')) {
echo __('You can not delete attachments.');
die;
}
}
add_filter( 'pre_delete_attachment', 'wp66410923_filter_attachment_deletion', 10, 3 );
function wp66410923_filter_attachment_deletion( $delete, $post, $force_delete ){
if (current_user_can('contributor')) {
return false;
}
return true;
}

How to hide all plugins installed in a WP site?

I want to hide all the plugins installed in my site. How I can achieve this. I tried with the following but it does not seem to work.
function hide_plugins($plugins)
{
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
foreach($plugins as $plugin){
if (in_array($plugin, array_keys($plugins))) {
unset($plugins[$plugin]);
}
}
return $plugins;
}
add_filter('all_plugins', 'hide_plugins');
Source : http://www.wpstuffs.com/hide-installed-plugins-from-dashboard-users-can-not-deactivate-the-plugin/
You don't need anything fancy.
add_filter('all_plugins', '__return_empty_array');
That does the trick for me.

Why won't WordPress load thickbox and media-upload scripts?

I'm working on a plugin that uses thickbox and media-upload to set some images. Neither will load using this code:
function add_my_files() {
echo 'happy happy happy';
wp_register_style( 'adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style( 'adminstyles' );
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs',plugins_url('/js/slider.js', __FILE__),array('media-upload','thickbox'),'',true);
wp_enqueue_script('hdjs');
}
add_action( 'admin_init', 'add_my_files' );
my css and js files load but not thickbox and media-upload.
Thanks
The correct hook to include your asset files in WP is admin_enqueue_scripts:
NOTE: I recommend you too use get_current_screen (see is_my_admin_screen() definition below) to just include your js/css files when you actually needed.
add_action('admin_enqueue_scripts', 'add_my_files');
function add_my_files()
{
/*
* a good WP citizen only loads
* their javascript/css where it is needed
*/
if ( ! is_my_admin_screen()) // defined below
return;
wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style('adminstyles');
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs', plugins_url('/js/slider.js', __FILE__), array('media-upload', 'thickbox'), '', true);
wp_enqueue_script('hdjs');
}
function is_my_admin_screen()
{
$screen = get_current_screen();
if (is_object($screen) && $screen->id == 'my_plugin_page_id') // var_dump($screen->id); find your own id
return true;
else
return false;
}
ref: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
ref: http://codex.wordpress.org/Function_Reference/get_current_screen
Besides hopefully you are using a class to wrap all your plugin or you will have worse problems than this.
Please feedback. I am very interested in this issue because WP plugins puts food and beers on my table.

Fatal error: Call to undefined function add_filter() in index.php in Wordpress

I want to change my wordpress theme dynamically by user browser. So I found this code on the net and added it to my index.php file
add_filter('template', 'switch_theme_by_browser');
add_filter('stylesheet', 'switch_theme_by_browser');
function switch_theme_by_browser($theme) {
$browser = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/MSIE/i',$browser) && !preg_match('/Opera/i',$browser))
{
$theme = "twentyeleven";
}
elseif(preg_match('/Firefox/i',$browser))
{
$theme = "twentyten";
}
elseif(preg_match('/Chrome/i',$browser))
{
$theme = "Boxoffice";
}
return $theme;
}
After that it shows me "Fatal error: Call to undefined function add_filter() in /home/xxx/public_html/domain.com/index.php on line 17"
As I undertand the "add_filter()" should be a function that is built in wordpress.
As Dawson said, this needs to go in your /wp-content/themes/[theme]/functions.php file.
In wordpress root directory, put require( ABSPATH . WPINC . '/plugin.php' ); before require( ABSPATH . WPINC . '/functions.php' ); in file wp-settings.php.
I verified this solution at http://wordpress.org/support/topic/fatal-error-call-to-undefined-function-add_filter.

Timthumb: Could not find the internal image you specified

I am having the following issue with this version of Timthum: 2.8.10 (running on Wordpress installation - server Ubuntu):
- When i call images from the server hosting the website i got this error:
A TimThumb error has occured
The following error(s) occured:
Could not find the internal image you specified.
Query String : src=http://my-host.it/wp-content/files_mf/1346848579prog_no_pic.png
TimThumb version : 2.8.10
If i copy/paste http://my-host.it/wp-content/files_mf/1346848579prog_no_pic.png in the browser i can get the image...
- When i call image from external sites it works fine.
I have enabled:
define ('ALLOW_ALL_EXTERNAL_SITES', TRUE);
at line 33.
For me, the document root wasn't being set correctly, due to my virtual host setup.
I had to add the correct one in timthumb-config.php
define('LOCAL_FILE_BASE_DIRECTORY', "/home/www/mysite/");
This two steps worked for me:
Set to true ALLOW_ALL_EXTERNAL_SITES, in line ~33:
if(! defined('ALLOW_ALL_EXTERNAL_SITES') ) define ('ALLOW_ALL_EXTERNAL_SITES', TRUE);
comment line ~212
//$this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
Source:
https://code.google.com/p/timthumb/issues/detail?id=363#c23
I just got over this problem which, in my case, had to do with the file path having a tilde in it.
Here's a link the solution I found:
http://elementdesignllc.com/2012/01/how-to-fix-timthumb-using-a-virtual-directory-url-contains-tildes/comment-page-1/#comment-7418
HTH!
I had the same problem on a Multisite installation of WordPress (3.5.1). The following fixed it:
In functions.php change
function get_image_url($img_src="") {
if($img_src!="") $theImageSrc = $img_src;
else $theImageSrc = wp_get_attachment_url(get_post_thumbnail_id($post_id));
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
echo $theImageSrc;
}
to
function get_image_url($img_src="") {
if($img_src!="") $theImageSrc = $img_src;
else $theImageSrc = strstr(wp_get_attachment_url(get_post_thumbnail_id($post_id)), "/wp-content");
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
echo $theImageSrc;
}
Note: The only actual modification happens in the 3rd line (bold):
$theImageSrc = strstr( wp_get_attachment_url(get_post_thumbnail_id($post_id)) , "/wp-content");
First let me know are you using WordPress Multisite? If yes then you dont need edit anything in timthumb.php me do like it
This is what I have done to get timthumb to work with multisite. You need to add this code to your theme's functions.php file
<?php function get_image_path($src) {
global $blog_id;
if(isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/' , $src);
if(isset($imageParts[1])) {
$src = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $src;
}
?>
And then where the timthumb script is used in the theme, you need to modify to this:
<img src="<?php bloginfo('template_url'); ?>/includes/timthumb.php?q=100&w=180&h=200&zc=1&a=t&src=<?php echo get_image_path(get_post_meta($post->ID, 'postImage', true)); ?>" alt="" />
where postImage is the name of the meta field the holds the image URL.
have a nice.

Resources