PHP code example of samiahmedsiddiqui / custom-permalinks
1. Go to this page and download the library: Download samiahmedsiddiqui/custom-permalinks library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
samiahmedsiddiqui / custom-permalinks example snippets
/**
* Add ACF field year and month in the permalink of the "Press" post type.
*
* @param string $custom_tag Custom tag name.
* @param string $post_type Post type from where it is called.
* @param object $post The post object.
*
* @return string Custom tag value which needs to be used.
*/
function custom_permalinks_post_permalink_tag( $custom_tag, $post_type, $post ) {
$custom_tag_value = $custom_tag;
if ( 'press' === $post_type &&
( 'year' === $custom_tag || 'month' === $custom_tag )
) {
// Replace the field name 'press_date'.
$press_date = get_field( 'press_date', $post->ID );
if ( ! empty( $press_date ) ) {
$date = new DateTime( $press_date );
} else {
$date = new DateTime( $post->post_date );
}
if ( 'year' === $custom_tag ) {
$custom_tag_value = $date->format( 'Y' );
} else {
$custom_tag_value = $date->format( 'm' );
}
}
return $custom_tag_value;
}
add_filter( 'custom_permalinks_post_permalink_tag', 'custom_permalinks_post_permalink_tag', 10, 3 );
function custom_permalinks_permalink_before_saving( $permalink, $post_id, $language_code ) {
// Check for a trailing slash in the permalink.
if ( '/' !== substr( $permalink, -1 ) ) {
// If the permalink doesn't have a trailing slash, add one.
$permalink .= '/';
}
return $permalink;
}
add_filter( 'custom_permalink_before_saving', 'custom_permalinks_permalink_before_saving', 10, 3 );
/**
* Generates custom permalinks for all posts within a specified post type.
*
* @param string $post_type The post type to generate permalinks for.
*/
function custom_permalinks_generate_permalinks_for_post_type( $post_type ) {
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1, // Get all posts of this type.
'post_status' => 'publish', // Only published posts.
'fields' => 'ids', // Only retrieve post IDs for efficiency.
);
$posts = get_posts( $args );
if ( $posts ) {
foreach ( $posts as $post_id ) {
do_action( 'custom_permalinks_generate_post_permalink', $post_id );
}
echo "Permalinks for all '{$post_type}' posts have been regenerated.";
} else {
echo "No '{$post_type}' posts found to regenerate permalinks for.";
}
}
// Example usage: Call the function to regenerate permalinks for the 'product' post type.
custom_permalinks_generate_permalinks_for_post_type( 'product' );
function custom_permalinks_exclude_post_types( $post_type ) {
// Replace 'custompost' with the name of the post type you want to exclude.
if ( 'custompost' === $post_type ) {
return '__true';
}
return '__false';
}
add_filter( 'custom_permalinks_exclude_post_type', 'custom_permalinks_exclude_post_types' );
function custom_permalinks_exclude_posts( $post ) {
// Replace '1557' with the ID of the post you want to exclude.
if ( 1557 === $post->ID ) {
return true;
}
return false;
}
add_filter( 'custom_permalinks_exclude_posts', 'custom_permalinks_exclude_posts' );
function custom_permalinks_xml_sitemap_url( $permalink ) {
if ( false !== strpos( $permalink, 'sitemap.xml' ) ) {
// Use '__true' to specifically ignore this permalink.
return '__true';
}
// Return null (or nothing) to allow default processing.
return;
}
add_filter( 'custom_permalinks_request_ignore', 'custom_permalinks_xml_sitemap_url' );
function custom_permalinks_avoid_redirect( $permalink ) {
// Always return true to disable all redirects.
return true;
}
add_filter( 'custom_permalinks_avoid_redirect', 'custom_permalinks_avoid_redirect' );
function custom_permalinks_avoid_redirect( $permalink ) {
// Replace 'testing-hello-world/' with the permalink you want to exclude from redirection.
if ( 'testing-hello-world/' === $permalink ) {
return true;
}
return false;
}
add_filter( 'custom_permalinks_avoid_redirect', 'custom_permalinks_avoid_redirect' );