Replace wp-content in image urls wordpress - php

I've changed my wp-content folder to a folder called stuff and am trying to change my image urls in existing posts with a function rather than a SQL query...
I've placed this in my function.php hoping it would work, but the images are still using the 'wp-content' folder?
define('WP_UPLOADSURL', 'http://' . $_SERVER['HTTP_HOST']);
add_filter( 'pre_option_upload_url_path', function( $upload_dir_uri ){
return str_replace( WP_UPLOADSURL . '/wp-content/', WP_UPLOADSURL . '/stuff/', $upload_dir_uri );
});

I just needed to add the filter to the_content not the pre_option_upload_url_path.
define('WP_UPLOADSURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
add_filter( 'the_content', function( $upload_dir_uri ){
return str_replace( WP_UPLOADSURL . '/wp-content/', WP_UPLOADSURL . '/stuff/', $upload_dir_uri ); /* Again Change stuff */
});

Related

use plugin_dir_path on a secondary php inside plugin folder

I'm trying to make a plugin to add a column to print some pdf files using the library fpdf using the code:
function rz_listing_pdf_column($columns) {
$columns['pdf'] = __( 'PDF', 'text_domain' );
return $columns;
}
add_filter( 'manage_rz_listing_posts_columns', 'rz_listing_pdf_column' );
function rz_listing_pdf_column_content($column, $post_id) {
if ( $column == 'pdf' ) {
$pdf_link = plugins_url('pdf.php?id=' . $post_id, __FILE__);
echo 'Download';
}
}
add_action( 'manage_rz_listing_posts_custom_column', 'rz_listing_pdf_column_content', 10, 2 );
this should open "pdf.php?id=" or print a PDF file using the post meta values of the clicked post, my issue it's that if I try to use
require_once plugin_dir_path(__FILE__) . 'fpdf/fpdf.php';
doesn't work, so neither I can't use:
$player1_birthday = get_post_meta($post_id, 'rz_player1_birthday', true);
because it's not reading anything coming from Wordpress, I'm new on this so I know I'm doing something wrong, any help?

Use an external Link as the WordPress profile picture

I want to write a Single Sign On (SSO) plugin for WordPress. I want to use an external image link as user's profile picture after user login.
This image has a link which is located in http://www.example.com/image.png.
How can I do this in WordPress?
Please add this code to your activated theme's functions.php file.
add_filter( 'author_link', 'modify_author_link', 10, 1 );
function modify_author_link( $link ) {
$link = 'http://google.com/';
return $link;
}
You can use your any link, just change https://google.com to your link.
Thanks.
These Hooks ('get_avatar', 'avatar_defaults') are also available by WordPress, as an example:
you can check the documentation here:
https://codex.wordpress.org/Function_Reference/get_avatar
function my_custom_avatar($avatar, $id_or_email, $size, $default, $alt)
{
echo $avatar . ' -> ' . $id_or_email . ' -> ' . $size . ' -> ' . $default . ' -> ' .$alt;
$avatar = 'https://www.example.com/yourImage';
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}
add_filter( 'get_avatar', 'my_custom_avatar', 10, 5 );
add_filter( 'avatar_defaults', 'my_custom_avatar', 10, 1 );```

Custom Upload Directory does not change attachment meta

I'm trying to make a download script for a password protected wordpress site. To make use of PHPs readfile() function I need to retrieve the full attachment URL based on it's ID i am passing to the download script.
I made a Custom Post Type named Downloads and also changed it's upload directory to a folder inside wp-content also named downloads.
Here is the code for it:
add_filter( 'upload_dir', 'custom_upload_directory' );
function custom_upload_directory( $args ) {
$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
if( "downloads" == get_post_type( $id ) || "downloads" == get_post_type( $parent ) ) {
$args['path'] = WP_CONTENT_DIR . '/downloads';
$args['url'] = WP_CONTENT_URL . '/downloads';
}
return $args;
}
Upload works fine and when I click the link to the desired file, the ID is passed to a script via $_POST, which also works fine. But I just can't figure out a way to get the right file URL. Here's what I tried:
wp_get_attachment_url( $id ); // returns: example.com/wp-content/uploads/html/theme/wp-content/downloads/filename.ext
wp_get_attachment_link( $id ); // returns: slug
get_attachment_link( $id ); // returns: example.com/downloads/file (without .ext)
get_attached_file( $id, true ); // returns: html/theme/wp-content/downloads/filename.ext
get_post_meta( $id, '_wp_attached_file', false ); // returns: html/theme/wp-content/downloads/filename.ext
wp_get_attachment_metadata( $id ); // returns nothing
What I expected any of those functions to return was example.com/wp-content/downloads/filename.ext
But as you can see, some mix up the default upload directory and combine it with the new one while others just return half of the full URL (html/theme/... it's the directory the website sits on the server). So any ideas would be appreciated.
Hours, days and even weeks later I finally found an answer and modified it to fit my needs. I came as far as displaying the right URL (the modified one) inside the file upload lightbox of Wordpress. But after publishing/updating the post it went back to the same old .../wp-content/uploads/file.ext URL.
Someone else, somewhere else got exactly the same problem and fortunately it is said there, that you must not just alter $args['path'] and $args['url'] but you also have to alter basedir, baseurl and subdir.
So, the complete code to change a custom post types upload directory (in this case I chose the directory .../wp-content/downloads) is the following:
add_filter( 'upload_dir', 'change_upload_dir' );
function change_upload_dir( $args ) {
$id = $_REQUEST['post_id'];
if( get_post_type( $id ) == 'downloads' ) {
$args['basedir'] = wp_normalize_path( WP_CONTENT_DIR . 'downloads' );
$args['baseurl'] = content_url() . '/downloads';
$args['path'] = $args['basedir'];
$args['url'] = $args['baseurl'];
$args['subdir'] = '';
return $args;
}
}
So now, calling wp_get_attachment_url() finally results in something like example.com/wp-content/downloads/file.ext

Why does home_url() not work with filemtime() in WordPress?

I wanted to start automatically changing my enqueue version so I don't have to manually change it after a file edit so I thought about using filemtime() to pull the time but for some reason when I use it with site_url() or home_url it doesn't work:
function bootstrap_enqueue() {
$bootstrap_file = home_url() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
but when I pass:
wp_enqueue_style('bootstrap-style', home_url() . '/css/bootstrap.css', '1.0' );
it works. I've researched and read:
Alternative to WordPress's get_site_url()
Get the last modified date of a remote file
Get last modified file in a directory
filemtime
but I haven't found an answer to why filemtime() it's work in WordPress with home_url?
EDIT:
Further testing I've tried:
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
thinking it might be a sequencing issue but still doesn't work. I've moved the CSS file into the theme's directory and tried:
$bootstrap_file = get_template_directory_uri() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
all of them are still producing the same result and the version is being pushed to 1.0.0 so I think it has something to do with $bootstrap_file = home_url() . '/css/boostrap.css'; just not sure what.
In the head I'm returned what appears to be correct:
<link rel='stylesheet' id='bootstrap-style-css' href='http://path/to/server/css/bootstrap.css?ver=1.0.0' type='text/css' media='all' />
but the Bootstrap file isn't rendering.
I think the reason is that PHP filesystem functions don't go off of URLs but absolute paths to files. So we need both the URL and the Absolute Path to the file so we can ensure it exists and get the file timestamp:
function bootstrap_enqueue() {
// Roots
// $bootstrap_abs = ASBSPATH . '/css/bootstrap.css';
// $bootstrap_url = home_url() . '/css/bootstrap.css';
$bootstrap_abs = get_stylesheet_directory() . '/css/bootstrap.css';
$bootstrap_url = get_stylesheet_directory_uri() . '/css/bootstrap.css';
if( file_exists( $bootstrap_abs ) ) :
$bootstrap_ver = date( "y.m.d", filemtime( $bootstrap_abs ) );
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_url, array(), $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
Close -- I believe the issue is that filemtime needs a file path asset, whereas you're trying to feed it a web URL.
I'd try something like using getcwd() to point to the file instead.
The 'home_url' function echos out the home URL, which will break your code. Use get_home_url() instead, because that function returns the value, which means you can store it in a variable or echo it.
wp_enqueue_style('bootstrap-style', get_home_url() . '/css/bootstrap.css', '1.0' );
EDITED
The PHP filemtime() function needs to get the files URL from the server root, but the wordpress wp_enqueue_style() function needs the host root URL. The following changes to your code might work as long as if your CSS is still in your root directory. If it is is your theme directory just change get_home_url() to get_template_directory_uri(). If that doesn't work, then smoke me a kipper, I'll be back for breakfast.
function bootstrap_enqueue() {
$bootstrap_file = get_home_url() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($_SERVER["DOCUMENT_ROOT"] . '/css/bootstrap.css'));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');

How to add "latin-extended" into a php query for a WordPress theme?

my title doesnt really tell anything. So I'll try to be clear. I am using a wordpress theme and it has google fonts pre-installed. However the included php for the fonts doesnt contain latin-extended option. How should I add the "&subset=latin,latin-ext" ?
The code follows:
foreach($googlefonts as $googlefont) {
if(!in_array($googlefont, $default)) {
$themename_customfont = str_replace(' ', '+', $googlefont). ':300,300italic,400,400italic,700,700italic,900,900italic|' . $themename_customfont;
}
}
if ($themename_customfont != "") {
function google_fonts() {
global $themename_customfont;
$protocol = is_ssl() ? 'https' : 'http';
wp_enqueue_style( 'themename-googlefonts', "$protocol://fonts.googleapis.com/css?family=". substr_replace($theretailer_customfont ,"",-1) . "' rel='stylesheet' type='text/css" );
}
add_action( 'wp_enqueue_scripts', 'google_fonts' );

Categories