Wordpress change media upload directory - wordpress

I manage to upload my medias using the below code. Unfortunately, the media are saving on the same month as the front-end form post date (/2014/09/). This is to upload the users profile image, not attached to any post. Hope someone can help out cause i couldnt find any resources online. Thank you!
// UPLOAD USER IMAGE
function upload_user_image($user, $nonce_name, $input_name, $field_name ) {
if (
isset( $_POST[$nonce_name] )
&& wp_verify_nonce( $_POST[$nonce_name], $input_name )
) {
$arr_file_type = wp_check_filetype(basename($_FILES[$input_name]['name']));
$uploaded_file_type = $arr_file_type['type'];
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
if(in_array($uploaded_file_type, $allowed_file_types)) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( $input_name, 0 );
if ( is_wp_error( $attachment_id ) ) {
$upload_feedback = 'There was a problem with your upload.';
} else {
$upload_feedback = false;
}
update_user_meta( $user, $field_name, $attachment_id );
} else {
$upload_feedback = 'Please upload only image files (jpg, gif or png).';
}
} else {
$upload_feedback = 'Security check failed';
}
}

Related

Add a suffix to all image URLs (File URL)

I am trying to add a suffix having certain values (such as min123) to all image URLs on my WordPress website getallnumber.com. The images are located in "/wp-content/uploads/year/month/". After adding the value the URL should be /wp-content/uploads/year/month/image.min123.png. I tried some pieces of codes including the sanitize code but there is no lock. Please help me out!
function sa_sanitize_special_chars ($filename) {
$f=remove_accents( $filename );
return 'min123-'.$f;
}
Add below code snippet in your active theme's functions.php file -
// wp media name suffix on upload
function modify_wp_handle_upload_prefilter( $file ) {
if( isset( $file['name'] ) ) {
$fileinfo = pathinfo( $file['name'] );
if( $fileinfo && isset( $fileinfo['filename'] ) && isset( $fileinfo['extension'] ) ) {
$suffix = 'min123'; // your suffix goes here
$name = $fileinfo['filename'] . '.' . $suffix . '.' . $fileinfo['extension'];
$file['name'] = $name;
}
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'modify_wp_handle_upload_prefilter', 99 );

Are WordPress admin plugins loaded on the front end?

My question basically is: are the "back-end" plugins, which in nothing affects the blog front-end, loaded when an anonymous user, for example, browse my blog?
Let's say the EWWW image optmizer, for instance: it just optmize the images, in the back-end. It has a admin interface to optimize the images, but the end-user doesn't use it at all. Still it gets loaded in each page visit?
I'm not sure if I'm making myself clear. Hope so.
As far as I know there is no way to specify a plugin as admin only in the WordPress API. The only plugin types I know about are 'must use', 'network activitate' (for multi user sites) and 'active' so I think an admin plugin will also load in non admin mode. The plugins are loaded in wp-settings.php. I read the code and it seems to me that WordPress doesn't distinguish between admin mode and non-admin mode as far as plugin loading is concerned. The relevant code is:
// Load must-use plugins.
foreach ( wp_get_mu_plugins() as $mu_plugin ) {
include_once( $mu_plugin );
}
unset( $mu_plugin );
// Load network activated plugins.
if ( is_multisite() ) {
foreach ( wp_get_active_network_plugins() as $network_plugin ) {
wp_register_plugin_realpath( $network_plugin );
include_once( $network_plugin );
}
unset( $network_plugin );
}
...
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
}
unset( $plugin );
function wp_get_mu_plugins() {
$mu_plugins = array();
if ( !is_dir( WPMU_PLUGIN_DIR ) )
return $mu_plugins;
if ( ! $dh = opendir( WPMU_PLUGIN_DIR ) )
return $mu_plugins;
while ( ( $plugin = readdir( $dh ) ) !== false ) {
if ( substr( $plugin, -4 ) == '.php' )
$mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
}
closedir( $dh );
sort( $mu_plugins );
return $mu_plugins;
}
function wp_get_active_network_plugins() {
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
if ( empty( $active_plugins ) )
return array();
$plugins = array();
$active_plugins = array_keys( $active_plugins );
sort( $active_plugins );
foreach ( $active_plugins as $plugin ) {
if ( ! validate_file( $plugin ) // $plugin must validate as file
&& '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
)
$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
}
return $plugins;
}
function wp_get_active_and_valid_plugins() {
$plugins = array();
$active_plugins = (array) get_option( 'active_plugins', array() );
// Check for hacks file if the option is enabled
if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
_deprecated_file( 'my-hacks.php', '1.5.0' );
array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
}
if ( empty( $active_plugins ) || wp_installing() )
return $plugins;
$network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
foreach ( $active_plugins as $plugin ) {
if ( ! validate_file( $plugin ) // $plugin must validate as file
&& '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
// not already included as a network plugin
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins ) )
)
$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
}
return $plugins;
}
Of course the easiest way to know for sure is to load a front end page on an WordPress installation with the suspect plugin and using the debugger to check if it loads.
Also, if a plugin was intended for admin use only the plugin author could just have
if ( ! is_admin() ) {
return;
}
at the start of the main plugin file. So the plugin essentially doesn't load.

Wordpress Updating Attachment Data

I'm attempting to add a feature to a plugin that extends media management. This feature would allow you to rename an attachment file. I've been able to complete this with the following code.
public function update_attachment_filename( $post_ID ) {
// Get path to existing file
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Generate new file name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the name and reference to file
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
}
While the original file gets renamed using the above method, all additional image sizes defined in the plugins/theme are not updated. I'm struggling to figure out the best way to accomplish this task.
I've looked into wp_update_attachment_metadata() and wp_generate_attachment_metadata() but am unsure whether they will give me the desired functionality.
Additionally I've looked into something such as:
$file_meta = wp_get_attachment_metadata( $post_ID );
foreach( $file_meta['sizes'] as $image ) {
// Do something
}
Any nudge in the right direction would be greatly appreciated.
Thanks!
I was able to utilize both the wp_generate_attachment_metadata() and the wp_update_attachment_metadata() function to achieve the desired end result.
public function update_attachment_filename( $post_ID ) {
if( isset( $_POST['update_filename'] ) && ! empty( $_POST['update_filename'] ) ) {
// Get path to existing attachment
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Create new attachment name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the attachment name
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
// Update attachment meta data
$file_updated_meta = wp_generate_attachment_metadata( $post_ID, $file_updated );
wp_update_attachment_metadata( $post_ID, $file_updated_meta );
}
}

notifications for buddypress Like plugin

I'm using the Buddypress like plugin which adds the feature to like status updates. Now when a user likes something it doesn't sent a status notification like other buddypress components do. I tried to edit the plugin's code to add this functionality and added the following code to bp-like.php, but still didn't work:
/******************************************************
/* Notification
*****************************************************/
function bp_like_setup_globals() {
global $bp, $current_blog;
$bp->bp_like->id = 'bp_like_notifier';
$bp->bp_like->slug = 'BP_LIKE_SLUG';
$bp->bp_like->notification_callback = 'like_format_notifications';
$bp->active_components[$bp->bp_like->slug] = $bp->bp_like->id;
do_action( 'bp_like_setup_globals' );
}
add_action( 'bp_setup_globals', 'bp_like_setup_globals' );
function like_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
global $bp;
$link=like_notifier_activity_get_permalink( $item_id );
if( 'activity_like' == $action ) {
if ( (int)$total_items > 1 )
return apply_filters( 'log_multiple_verifications_notification', '' . sprintf( __('You have %d new likes', 'bp-like' ), (int)$total_items ) . '', $total_items );
else
return apply_filters( 'log_single_verification_notification', '' . sprintf( __('You have %d new like', 'bp-like' ), (int)$total_items ) . '', $total_items );
}
do_action( 'like_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
return false;
}
function like_remove_screen_notifications() {
global $bp;
if($has_access)//if user can view this activity, remove notification(just a safeguard for hidden activity)
bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, $bp->bp_like->id, 'new_like' );
}
add_action( 'bp_activity_screen_single_activity_permalink', 'like_remove_screen_notifications' );
//get the thread permalink for activity
function like_notifier_activity_get_permalink( $item_id, $activity_obj = false ) {
global $bp;
if ( !$activity_obj )
$activity_obj = new BP_Activity_Activity( $item_id );
if ( 'activity_comment' == $activity_obj->type )
$link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->item_id . '/';
else
$link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->id . '/';
return apply_filters( 'like_notifier_activity_get_permalink', $link );
}
and inside bp_like_add_user_like() function I put this after $liked_count = count( $users_who_like ):
bp_core_add_notification( $item_id, $user_id, $bp->bp_like->id, 'activity_like' );
so far this didn't work.. any idea what I'm missing here?
thanks

code to activate plugin in wordpress

This client wants to automatically activate a wordpress plugin every Tuesday between some hours. This is because the plugin has conflicts with another plugin. I did not find anything on the net about that, how to do it... anyone knows what happens behind wordpress when the button activate plugin is clicked? I can't find that specific page in my wordpress folder...
Thanks!
Something that I tried and not working:
require('/web/htdocs/www.fattorefamiglia.com/home/wp-content/plugins/quick-chat/quick-chat.php');
function toggle_plugin() {
// Full path to WordPress from the root
$wordpress_path = '/web/htdocs/www.fattorefamiglia.com/home/';
// Absolute path to plugins dir
$plugin_path = $wordpress_path.'wp-content/plugins/';
// Absolute path to your specific plugin
$my_plugin = $plugin_path.'quick-chat/quick-chat.php';
$start = strtotime('1:30');
$end = strtotime('22:30');
$timenow = date('U');
if((date('w') == 3) && ($timenow >= $start && $timenow <= $end)) { // day 2 = Tuesday
activate_plugin($my_plugin);
}
else {
deactivate_plugins($my_plugin);
}
}
I put this code in functions.php
Activate Plugin by Code in wordpress
function run_activate_plugin( $plugin ) {
$current = get_option( 'active_plugins' );
$plugin = plugin_basename( trim( $plugin ) );
if ( !in_array( $plugin, $current ) ) {
$current[] = $plugin;
sort( $current );
do_action( 'activate_plugin', trim( $plugin ) );
update_option( 'active_plugins', $current );
do_action( 'activate_' . trim( $plugin ) );
do_action( 'activated_plugin', trim( $plugin) );
}
return null;
}
run_activate_plugin( 'plugin-folder-name/plugin-main-file.php' );

Resources