I currently have this page /free-consultation-sp/ on wordpress
I'm trying to create a dynamic button on that page to go to
/free-consultation/ by just removing the last 3 characters "-sp"
here is my current code, which only return the current slug
button:
English
function.php
function the_slug_english() {
global $post;
$slug = get_post( $post )->post_name;
return $slug; }
Is there a way to do this in wordpress? Thank you very much
One simple approach is to use substr:
function the_slug_english() {
global $post;
$slug = get_post( $post )->post_name;
if (substr($slug, -3) == '-sp') {
$slug = substr($slug, 0, strlen($slug-3));
}
return $slug;
}
This will strip off -sp from the slug. The substr($slug, -3) gets the last 3 characters from the $slug.
Related
I am trying to customise a title for a Wordpress page. In the page I have some php to obtain data from the database. I would like to output a custom title based on the data and the page id. I have tried this:
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title() {
global $post;
$postid = $post->ID;
if ($postid == '72616') {
$title['title'] = "Property number $propertyid";
return $title;
}
}
How can I check the postid and if it is the right page, output the data from the sql query?
Many thanks,
Neil.
The title is passed in for that filter. Make sure you return title outside of the if statement.
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title($title) {
global $post;
$propertyid = get_query_var('reference');
$postid = $post->ID;
if ($postid == '72616') {
$title = "Property number $propertyid";
}
return $title;
}
add_filter( 'query_vars', 'add_property_query_vars' );
function add_property_query_vars( $query_vars ) {
$query_vars[] = 'reference';
return $query_vars;
}
In my wordpress theme i have included meta box for Title, description and redirect section. I have a input field for the redirect box under every post and page id. If i add any url to a particular page/post id for that redirect box, the particular page/post id should redirect to my own url given in that redirect box. Is there any function to do this? I have searched in stackies and i get the following code. But its not working.
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
}
return $permalink;
}
add_filter('get_the_permalink','my_permalink_redirect');
Try:
add_filter( 'the_permalink', 'filter_function_name_7062', 10, 2 );
function filter_function_name_7062( $permalink, $post ){
global $post;
if ($post->ID == 684) {
$permalink = 'http://sample.com/1';
}
if ($post->ID == 444) {
$permalink = 'http://sample.com/2';
}
return $permalink;
}
Try:
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
wp_redirect("'.$permalink.'", 301);
exit;
}
}
add_filter('get_the_permalink','my_permalink_redirect');
I have a function that gets the overall word count within a post.
function ic_word_count() {
global $post;
$ic_content = strip_tags( $post->post_content );
$ic_stripped = strip_shortcodes($ic_content);
return $ic_stripped;
}
I'm trying to get it to exclude all shortcodes, so basically exclude anything with square brackets and everything in between them. For example exclude [shortcode] or [This Shortcode]
Any idea on how I could add that part to the above function?
WordPress has a handy function, strip_shortcodes. Codex details here: https://codex.wordpress.org/Function_Reference/strip_shortcodes
Your function uses $post->ID but you aren't getting the global post value.
Finally you already have access to the post content inside the global $post object so just use that instead of get_post_field.
E.g. global $post;
function ic_word_count() {
global $post;
$wc_content = $post->post_content;
$ic_word_count = str_word_count( strip_tags( strip_shortcodes( $wc_content ) ) );
return $ic_word_count;
}
A smaller version:
function ic_word_count() {
global $post;
return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
}
I need to show excerpts of different lengths, so I use
function custom_excerpt($length) {
get_the_content();
... clean up and trim
return $excerpt;
}
however, I want to detect if a manual excerpt was entered in order to use that instead of the custom one. Is there a way to do this?
I tried by using
$wp_excerpt = get_the_excerpt();
But that returns the manual excerpt, and if the manual excerpt is empty, it automatically generates an excerpt of 55 characters, which doesn't help, because it will always be "true" (can't check if empty).
The reason for approaching it this way is because I have multiple excerpts on a single page (different lengths), and if the length needed is longer than the WordPress excerpt (55), I want to show my excerpt, unless a manual excerpt was written, in which case I want to show that.
It would be perfect if I could simply
if ( manual_excerpt() == true ) {
}
You need only replace the excerp_length wordpress default function, just follow the above code, then you can call this custom function and set the length:
<?php
// custom excerpt length
function custom_excerpt_length( $length = 20 ) {
return $length;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
?>
ANSWER UPDATED II
Using inside a function:
<?php
function custom_excerpt( $length = 55 ) {
if( $post->post_excerpt ) {
$content = get_the_excerpt();
} else {
$content = get_the_content();
$content = wp_trim_words( $content , $length );
}
return $excerpt;
}
?>
This is an old question but I was looking for this, ended up here and didn't see the following function in the answers. To know if a post has a custom excerpt you can use the has_excerpt function:
<?php has_excerpt( $id ); ?>
Where $id is the post id. If non is given then the current post id will be used.
Check if the post_excerpt slot in the post object is empty or not:
global $post;
if( '' == $post->post_excerpt )
{
// this post does NOT have a manual excerpt
}
To turn this into a function:
function so19935351_has_manual_excerpt( $post )
{
if( '' == $post->post_excerpt )
return false;
return true;
}
When we add new post in wordpress, after supplying the post title, the slug is generated automatically. I need to edit that auto generation module so that i can add some arbitrary number in the end of the slug automatically. How to do it?
Don't use the hard-coded version that the OP used here. When he did that, there was not a filter available. More recently, since 3.3, a filter was added.
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$slug = md5( time() );
}
return $slug;
}
However this method will change the slug every time you save the post... Which was what I was hoping for...
EDIT:
This kind of works for limiting the generation to just once. The only drawback is that it creates one version when ajax runs after creating the title, then it creates another, permanent slug when the post is saved.
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$post = get_post($post_ID);
if ( empty($post->post_name) || $slug != $post->post_name ) {
$slug = md5( time() );
}
}
return $slug;
}
Write a plugin to hook into the wp_insert_post_data filter so you can update the slug before the post is sent for insertion into the database:
function append_slug($data) {
global $post_ID;
if (empty($data['post_name'])) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . generate_arbitrary_number_here();
}
return $data;
}
add_filter('wp_insert_post_data', 'append_slug', 10);
Note that this function requires that you allow WordPress to auto-generate the slug first, meaning you must not enter your own slug before generating, and it cannot update existing posts with the number.
Test this : (paste it into functions.php)
function append_slug($data) {
global $post_ID;
if (!empty($data['post_name']) && $data['post_status'] == "publish" && $data['post_type'] == "post") {
if( !is_numeric(substr($data['post_name'], -4)) ) {
$random = rand(1111,9999);
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . $random;
}
}
return $data; } add_filter('wp_insert_post_data', 'append_slug', 10);
add_filter('post_link','postLinkFilter', 10, 3);
/**
* Manipulates the permalink
*
* #param string $permalink
* #param stdClass $post
* #return string
*/
function postLinkFilter($permalink,stdClass $post){
return $permalink.'?12345';
}
Untested in that scenario, but I have already used it, should work with a minimum of changes, but try and test it REALLY Carefully .
In any Case, don't use rand() here or something alike, since the function must return the same link for the same post every time, otherwise you will have some serious problems.
Have fun!
You should operate with wp_ajax_sample-permalink action and name_save_pre filter.
More examples here: https://wordpress.stackexchange.com/a/190314/42702