What I'm trying to do is change the link to the main attachment page in Wordpress. Basically, I'm trying to change the word attachment to media.
I'm trying to change:
example.com/parent-category/child-category/post-slug/attachment/attachment-name/
to:
example.com/parent-category/child-category/post-slug/media/attachment-name/
Thanks in advance for any help on this.
I know this is an old question, but I just stumbled on it and thought it was worth a shot;
function __filter_rewrite_rules( $rules )
{
$_rules = array();
foreach ( $rules as $rule => $rewrite )
$_rules[ str_replace( 'attachment/', 'media/', $rule ) ] = $rewrite;
return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );
function __filter_attachment_link( $link )
{
return preg_replace( '#attachment/(.+)$#', 'media/$1', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );
Related
I have a CPT called quotes with ACFs that I want to add to my RSS feed.
My rss url is example.com/feed/?post_type=quotes
When I use is_feed() this code works. But it doesn't work when I try to limit it to only the quotes CPT using: is_feed('quotes')
function add_fields_to_rss ($content) {
if(is_feed('quotes')) {
$post_id = get_the_ID();
$output = '<div><p>' . get_field('the_quote', $post_id) . '</p>';
$output .= '<h3 style="text-align: right;">' . get_field('quoted', $post_id) . '</h3>';
$output .= '</div>';
$content = $content.$output;
}
return $content;
}
add_filter('the_content','add_fields_to_rss');
Is there anything else I need to do so that this function will work with my quotes CPT only?
if I understand currently you want to add all custom posts types to the rss feed?
If so the following should work:
add_filter( 'request', 'myfeed_request' );
function myfeed_request( $qv ) {
if ( isset( $qv['feed'] ) ) {
// gett all custom posts types
$qv['post_type'] = get_post_types();
}
return $qv;
}
Tell me if this is what you're looking for
EDIT 1: Aswering your updated question and comment
The following is for adding specific customs posts types to the rss feed
<?php add_filter( 'request', 'multiple_CPT_to_rss' );
function multiple_CPT_to_rss( $qv ) {
if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) ) {
$qv['post_type'] = array( 'post', 'my_CPT_1', 'my_CPT_2' );
}
return $qv;
}; ?>
CPT creates default rss links in the form of My rss url is example.com/feed/?post_type=my_post_type
If seems that doesn't work with is_feed('my_post_type')
I'm guessing to make it work you need to create the feed with add_feed()
But I didn't test that because I went another way.
I am wondering if you're able to help me with the following WordPress customization. We're using the WP Job Manager plugin (https://wpjobmanager.com/) and I'd need a little help with a slug/permalink edit.
In the documentation is an article available which explains the following: at the current situation links are generated as follows: domain.com/job/job-name. However, I need the following structure: domain.com/job-category/job-name.
Please check: https://wpjobmanager.com/document/tutorial-changing-the-job-slugpermalink/
The article explains this. Please check the code on: Example: Adding the category to the base URL. When I remove the 'job' in the following code, the job listings are working fine, but the rest of my website returns in a 404 error (also after saving the permalinks).
$args['rewrite']['slug'] = 'job/%category%';
To
$args['rewrite']['slug'] = '%category%';
Full code:
function job_listing_post_type_link( $permalink, $post ) {
// Abort if post is not a job
if ( $post->post_type !== 'job_listing' )
return $permalink;
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;
// Get the custom taxonomy terms in use by this post
$terms = wp_get_post_terms( $post->ID, 'job_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) );
if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$job_listing_category = _x( 'uncat', 'slug' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$job_listing_category = $first_term->slug;
}
$find = array(
'%category%'
);
$replace = array(
$job_listing_category
);
$replace = array_map( 'sanitize_title', $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;
}
add_filter( 'post_type_link', 'job_listing_post_type_link', 10, 2 );
function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = 'job/%category%';
return $args;
}
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );
Well, I was also stuck in some type of error once. But in my case, I was creating everything my self. So I change the template accordingly.
However, In you case, I think it relates to URL Re-Writing. Please check out the following links. I hope those will help.
http://www.wpbeginner.com/plugins/how-to-change-custom-post-type-permalinks-in-wordpress/
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
https://paulund.co.uk/rewrite-urls-wordpress
I am trying to change link to attachment page in wordpress for two days. i am close to resign right now...
What i have right now is:
mysite.com/?attachment_id=1234
What I need is:
mysite.com/{postname}/image/{attachment_id}
I have found something at stack
function __filter_rewrite_rules( $rules )
{
$_rules = array();
foreach ( $rules as $rule => $rewrite )
$_rules[ str_replace( 'attachment', 'media', $rule ) ] = $rewrite;
return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );
function __filter_attachment_link( $link )
{
return str_replace( 'attachment', 'media', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );
But actually it do not change anything. I am not able to enter page with attachment using link:
mysite.com/media_id=123
Please explain me why?
I'm trying to add a custom default avatar to WordPress in functions.php, but the image is not displaying in Settings/Discussion or elsewhere on the site. The code works because a new radio field is added with the custom field name, but the image won't display. Is the avatar not displaying because I'm using Localhost?
I don't have enough reps to comment on similar questions.
here's the code:
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
$new_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.png';
$avatar_defaults[$new_avatar] = "Default Avatar";
return $avatar_defaults;
}
I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.
I was facing the same issue and came up with this completely hackish solution... It works though :)
add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 );
function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
$whitelist = array( 'localhost', '127.0.0.1' );
if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) )
return $avatar;
$doc = new DOMDocument;
$doc->loadHTML( $avatar );
$imgs = $doc->getElementsByTagName('img');
if ( $imgs->length > 0 )
{
$url = urldecode( $imgs->item(0)->getAttribute('src') );
$url2 = explode( 'd=', $url );
$url3 = explode( '&', $url2[1] );
$avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='64' width='64' />";
}
return $avatar;
}
Result:
Of course, this filter is meant for development only.
I want to use something like:
http://example.com/%category_id%/%postname%/
for the permalink structure.
For example, if a post has a category with an ID of 3, then the URL for the post will be
http://example.com/3/post-name/
Does anyone know how this can be done? I don't mind modifying WordPress core.
This code adds the %category_id% rewrite tag, and filters post permalinks to replace them with the actual category ID (lowest if there are multiple categories). You can place this in a plugin or in your theme file.
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}