It’s because you’re not actually putting the result of ltrim()
and rtrim()
back into the variable. Those functions return the trimmed value, they don’t modify the passed variable. So you need to do this:
$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));
$post_url_rel = ltrim($post_url_rel, "https://wordpress.stackexchange.com/");
$post_url_rel = rtrim($post_url_rel, "https://wordpress.stackexchange.com/");
Or better yet, just use trim()
, which will remove it from both ends:
$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));
$post_url_rel = trim($post_url_rel, "https://wordpress.stackexchange.com/");
Related Posts:
- Custom Post Type, post-formats associated to custom post type > 404 archives
- How to bulk Update URLs to new values?
- Template for displaying CPT / Taxonomy URL issues
- Custom URL Structure for posts with subcategories
- Is there a Standard Format for Clean URLs for Archives of Custom Posts?
- Choose options via url
- Default post type doesn’t display in url
- How to display by default only published posts/pages in the admin area?
- Access post from post id in URL
- How to Check if a Page Exists by URL?
- Find out who deleted a page or post?
- Post/Page Preview Template
- using wp_update_post on save_post
- Replace image attributes for lazyload plugin (data-src)
- WordPress plugin to publish to multiple remote WordPress blogs
- Any way to create a revision of a post ONLY with a new button in the Meta Box?
- How do you get all the urls of images attached to a post?
- How to display comments and comment form on custom post type?
- How to wrap every image in a post with a div?
- Show One Category on Post
- how to grab first link in post… and of course call it
- WordPress Delete hook with wp_delete_post function?
- Custom post type single (permalink) throws a 404 error
- Order post by year DESC and month ASC
- Do I use custom post type or something else?
- Delete Post From Front-End and attachment permanently
- if custom post type exist
- Custom post type permalinks breaking due to 3.1 bug fix
- ‘Invalid post type’ error when importing from one site to another
- Blogroll – Different layout for first post
- How to delete old posts’ edits to save space?
- Get post id in wordpress action?
- Animated gif for a thumbs
- Edit custom post type admin menu link
- I am unable to publish/update post
- How to include a post type in the admin/backend?
- Paginated Posts – Social Media Buttons Share First Page URL
- Get posts of ONE taxonomy term of custom post type
- WordPress not opening posts with only numbers if permalink is post_name
- WordPress Custom Fields (Checkboxes with multiple values)
- Using custom post template
- Custom Post Archive Page not working
- Assign category to new post via URL
- Adding custom fields to bbpress reply form
- Adding buttons to Add New Post and Add New Page
- filter RSS feed in URL
- Editing the default page to show all posts, rather than most recent ones
- Permission issue with custom post type – not added to menu – by plugin
- Remove Custom Post Type Slug and add Custom Taxonomy to Permalink Structure?
- Add a custom meta box in the post options that loads some html code in the header
- Static posts page with home.php
- Custom post types related to same custom post type?
- Is it possible to customize the post according to post format in single.php?
- Using setup_postdata() with multi-dimensional array
- Check if a custom post type has already been created
- Make insterted image point to post url instead of attachment page
- Display the 2nd category name of a custom post type without error if its null?
- Querying another post category to match current post and display in loop
- datepicker value in mysql
- Simple Custom Metabox Not Saving
- How to restore deleted post related with deleted users
- Custom Post Type Single Loop Outputting Wrong Post
- Organization of time insensitive content
- Can I style single post that are in multiple catergories?
- WordPress Post Deletion Issue
- How Can I Always Display A Particular Post First Using WP_Query?
- MySQL: Possible to replace all of wp_posts.post_content(id#) via UPDATE + REPLACE + SELECT?
- Change modified date to current date when title updated automatically
- WordPress posts have a different URL to the index
- How to put the author of the post in the comments?
- How to add a custom class attribute into code wrapper? [duplicate]
- Redirecting wordpress search query to archive page
- How to create custom search form function including post “tags”
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- How to invoke a HTML custom button based on a HTML dropdownlist menu
- Please I want to prefix my WP posts title according to each category
- insert post fails after multiple postings
- Custom Post Type Rewrite URL
- Custom Post Types and Broken Permalinks
- The first post appears twice
- Customizing URL slugs of Custom Post Type and Taxonomy make posts/pages 404
- Append a random string to a post permalink
- Multipage Post URL correction
- Show Posts By Custom Field
- Custom Post Type doesn’t use single.php or single-{custom_post_type}.php
- How to retrieve custom field values and save again as array
- How to change post template via url?
- Checking if some condition is matched before inserting post into database
- How to only allow post to be deleted if custom field doesn’t exist
- Moving Posts to a new site and maintaining URLs
- Change the default video URL in a post to shortcode format
- Custom Post Title as search term
- adding custom meta as well as looping through posts
- Best way to enable sorting by 3 criteria on a page of listings
- How remove “recent comments” title without modifying code? [closed]
- How edit footer copyright info? [closed]
- Need to show birthday of custom post type(Famous people in this case) in elementor. Date of birth set in custom fields
- Post page still linked to old site
- Blog posts are not appearing as subs of Blog Page
- How to add paraent in Blog post URL in wordpress