blank page when trying to change upload directory - php

Working with wordpress 4.2.2, when I use this code in child theme function.php to change the attachments upload directory according to each post type :
function wpse_16722_type_upload_dir( $args ) {
// Get the current post_id
$id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
if( $id ) {
// Set the new path depends on current post_type
$newdir = '/' . get_post_type( $id );
$args['path'] = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
return $args;
}
}
add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );
I get a blank page!!
there is no empty rows in the function.php and the problem appears only after adding this code. I really need it to organize my upload folder, but without a blank page.
is there any solution?

As pointed out above, you need to see the error reporting. Add to the top of wp-config.php:
php error_reporting(E_ALL); ini_set('display_errors', 1);
If that's not working, find define('WP_DEBUG', false); and set the value to true
This might also help, add to the top of wp-config to pipe errors to err.log at your document root:
#ini_set( 'log_errors', 'On' );
#ini_set( 'display_errors', 'Off' );
#ini_set( 'error_log', $_SERVER['DOCUMENT_ROOT'] . '/err.log' );

Related

wordpress get url of registered script

If I register a script or style (using wp_register_script() or wp_register_style()), is there a way I can get the URL of that script/style?
(If you must know why, I'm trying to put those URL's into another function that generates prefetch link tags so I can prefetch certain scripts/styles for a performance boost in my site.)
Just in case someone is still looking for this:
<?php
function invdr_get_script_uri_by_handler( $handler ){
//Get an instance of WP_Scripts or create new;
$wp_scripts = wp_scripts();
//Get the script by registered handler name
$script = $wp_scripts->registered[ $handler ];
if ( file_exists( ABSPATH . $script->src ) ){
return ABSPATH . $script->src;
}
return false;
}
add_action( 'wp_enqueue_scripts', 'invdr_get_script_uri_by_handler', PHP_INT_MAX );
Tested in wordpress 5.0
You can use wp_scripts() to get the instance of the WP_Scripts class which contains the registered scripts (this class extends WP_Dependencies).
Basically, try looking in:
$wp_scripts = wp_scripts();
var_dump( $wp_scripts->registered );
var_dump( $wp_scripts );
Here's how I've accomplished this in a self-authored plugin to help me enhance dependencies within WordPress:
// Convert relative URL to absolute?
$absolute = true;
$handle = 'your_stylesheet_handle';
$helper = wp_styles();
$object = $helper->registered[ $handle ];
$src = $object->src;
if (
$absolute
&& $helper->in_default_dir( $src )
) {
$src = $helper->base_url . $src;
}
$ver = $object->ver;
if ( ! is_null( $ver ) && empty( $ver ) ) {
$ver = $helper->default_version;
}
if ( isset( $helper->args[ $handle ] ) ) {
$ver = $ver ? $ver . '&' : '';
$ver .= $helper->args[ $handle ];
}
$src = add_query_arg( 'ver', $ver, $src );
$stylesheet_url = urldecode_deep( $src );
Note that the absolute URL conversion is directed towards handling assets registered by WordPress core, as they're typically relative URLs.

wordpress upload file error

I have this upload file system in wordpress and everything is working fine but the file wont go into the folder. Here's what i have right now:
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
// Change your upload directory
function my_upload_dir(){
return PLUGIN_DIR . '/uploads/';
}
// Register our path override.
add_filter( 'upload_dir', 'my_upload_dir' );
// Set where to get the file from
$uploadedfile = $_FILES["attach"];
$upload_overrides = array( 'test_form' => false );
// Do the file move
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
// Set everything back to normal.
remove_filter( 'upload_dir', 'my_upload_dir' );
// Return an error if it couldn't be done
if (!$movefile || isset( $movefile['error'])) {
echo $movefile['error'];
}
its seems to be working fine (no errors) but the image wont show in the folder.
any help would be appreciated.
I think This code is working Fine.
$date=strtotime(date('Y-m-d H:i:s'));
$pro_image_name = $date.$_FILES['your Input type File Name']['name'];
$allowed = array('gif','png','jpg');
$ext = pathinfo($pro_image_name, PATHINFO_EXTENSION);
$root_path = get_template_directory();
if(!in_array($ext,$allowed) ) { ?>
<span style="font-size:22px; color:red;">Uploaded File Not Supported</span>
<?php } else {
move_uploaded_file($_FILES['updateimg']['tmp_name'],$root_path."/images/".$pro_image_name);
$image_get_path = site_url()."/wp-content/themes/prathak/images/".$pro_image_name;
update_user_meta(get_current_user_id() , 'userpic' , $image_get_path );
}
Good Luck

Wordpress - upload files and rename with custom user meta

when my users upload a file, I need to rename it using a specific user_meta value. So, using wp_handle_upload I've set a callback function for $upload_overrides like this:
$upload_overrides = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );
and my callback function is
function change_document_name($dir, $name, $ext){
global $current_user;
$doc_type = get_user_meta($current_user->ID, 'document_type', true);
return $doc_type . '_mydoc' . $ext;
}
Now as you can see, we are talking about users documents so I need to rename them according to the document type they've uploaded. For example, if they've uploaded a "passport" and selected the document type (of course), I should get the user_meta 'document_type', use it as prefix and place it in front of the filename uploaded, outputting something like
passport_mydoc.pdf
Of course my function doesn't work and I don't understand why it doesn't take the global $current_user or at least if there is some other method to accomplish this.
Many thanks.
EDIT
To explain it better, my fault, the function change_document_name() does rename the file like so:
_mydoc.ext (e.g _mydoc.pdf)
This means that the function is correctly called and runs, except for the first part ignoring $doc_type variable. For this reason I suppose that the $current_user it's not working. My complete code to upload the file is the following:
if(!empty($_FILES['docfile'])):
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
$upload_overrides = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );
add_filter('upload_dir', 'my_user_folder'); //A documents custom folder
$uploaded_file = wp_handle_upload($_FILES['docfile'], $upload_overrides);
remove_filter( 'upload_dir', 'my_user_folder' );
$doc_file_loc = $uploaded_file['file'];
$doc_file_title = $_FILES['docfile']['name'];
$doc_file_arr = wp_check_filetype(basename($_FILES['docfile']['name']));
$doc_file_type = $doc_file_arr['type'];
$doc_file_att = array(
'post_mime_type' => $doc_file_type,
'post_title' => addslashes($doc_file_title),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => 0,
'post_author' => $uid
);
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$doc_file_id = wp_insert_attachment( $doc_file_att, $doc_file_loc, 0 );
$doc_file_url = wp_get_attachment_url( $doc_file_id );
update_user_meta($uid,'document_file', $doc_file_url);
endif;
The hook 'unique_filename_callback' is used according to the codex here https://developer.wordpress.org/reference/functions/wp_unique_filename/
I'm not sure why the global isn't returning the user. It should be. But try get_current_user_id() and see if it works:
function change_document_name( $dir, $name, $ext ){
if ( ! is_user_logged_in() ) {
error_log( "User not logged in." );
return;
}
$user_id = get_current_user_id();
// Uncomment to see if there is any value
// var_dump( $user_id );
$doc_type = get_user_meta( $user_id, 'document_type', true );
// Uncomment to see if there is any value
// var_dump( $doc_type );
if ( ! $doc_type ) {
error_log( "There is no doc type set for the current user with id $user_id" );
return;
}
return $doc_type . '_mydoc' . $ext;
}
I have added some var_dumps in there you can use to see what values are being returned, or you can debug if you have xdebug set up. But this should give you what you need. You can also remove the error logging if you don't want to log those errors. They are there so you can check the site logs and see what is in them.

fatal error: call to a member function delete() on a non-object wordpress

I am trying to delete rows from db and getting this error. I googled and tried all the possible solution still no luck. I also mentioned "global $wpdb" but dont know why getting this error.
<?php
if($_POST['array'])
{
global $wpdb;
$productArray = $_POST["array"];
$count = count($productArray);
$table_name = "wp_cause_woocommerce_product";
for( $i=0; $i < $count; $i++ ){
$wpdb->delete( $table_name, array( 'product_ID' => $productArray[$i] ), array( '%d' ) );
}
}
I think the issue is that you are not getting the WordPress functions.
Add this in to the top of your code.
define('WP_USE_THEMES', false);
require_once( $_SERVER['DOCUMENT_ROOT'] . '/fundraise/wp-load.php' );
If you change your project path make sure you update the path to wp-load.php.
If you will change your project path in future, you can try this
define('WP_USE_THEMES', false);
require_once( dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/wp-load.php');
Hope this helps.
I just add the below lines on top of the page and its working. I had to relate the file with wp functionality thats it :
define('WP_USE_THEMES', false);
require_once( $_SERVER['DOCUMENT_ROOT'] . '/fundraise/wp-load.php' );

Wordpress - 'strpos() empty needle' and 'cannot modify header' warnings

Months ago, I have placed a 301 redirect rule in my .htaccess file to redirect all the www request to a non-www request.
The problem is two days ago, when I tried to access my example.net site using www.example.net I get the following warnings in the page and website is not loaded.
http://i.stack.imgur.com/nXBMF.png
Here are the corresponding lines:
1. Plugin.php Line 647 = if ( strpos( $file, $realdir ) === 0 ){
Full function:
/**
* Gets the basename of a plugin.
*
* This method extracts the name of a plugin from its filename.
*
* #since 1.5.0
*
* #param string $file The filename of plugin.
* #return string The name of a plugin.
*/
function plugin_basename( $file ) {
global $wp_plugin_paths;
foreach ( $wp_plugin_paths as $dir => $realdir ) {
if ( strpos( $file, $realdir ) === 0 ) { /** LINE 646 */
$file = $dir . substr( $file, strlen( $realdir ) );
}
}
$file = wp_normalize_path( $file );
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
$file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
$file = trim($file, '/');
return $file;
}
2. Pluggable.php Line 1178 = header("Location: $location", true, $status);
Full file: http://pastebin.com/0zMJZxV0
I use WordPress only to write some articles. My PHP knowledge is very basic and limited only to locate errors.
Please help me figure out the problem with this. As I have read from the Codex FAQ, they say that empty strings may be a culprit for the pluggable.php error. But I have no idea how to locate it and I have attached the file for your reference.
Please provide your suggestions to avoid this error in the future. Thanks in advance.
3. EDIT - wp setting file: (the error line - include_once( $plugin ); )
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
}
unset( $plugin );
The issue with the header information has been discussed in here: Cannot modify header information error in Wordpress. Could you give this a try and see whether this solves this part of your problem?.
On the other issue:
try var_dump ($file) (for instance -- or echo $file ) to see what they actually contain.
Check your configuration path of plugins :
var_dump($wp_plugin_paths);
You've got an error because $realdir is empty.

Categories