Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created August 16, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BhargavBhandari90/4658cbbd03c6c16f367f6619a7b07dbe to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/4658cbbd03c6c16f367f6619a7b07dbe to your computer and use it in GitHub Desktop.
Replace media ID with the media title in URL
<?php
// Add this code into theme's functions.php
function custom_get_rtmedia_permalink( $permalink, $media, $id ) {
$post = get_post( $media[0]->media_id );
$title = isset( $post->post_name ) ? $post->post_name : '';
if ( ! empty( $title ) ) {
$permalink = str_replace( '/' . $id , '/' . $title, $permalink );
}
return $permalink;
}
function custom_rtmedia_action_query_modifier_type( $modifier_type, $raw_query ) {
$allowed_types = rtmedia_get_allowed_types();
$allowed_types = array_keys( $allowed_types );
if ( is_array( $raw_query ) && array_key_exists( '0' , $raw_query ) ) {
$post = custom_get_page_by_slug( $raw_query[0], OBJECT, 'attachment' );
$post_album = custom_get_page_by_slug( $raw_query[0], OBJECT, 'rtmedia_album,rtmedia_playlist', 'publish,hidden' );
$post = $post ? $post : $post_album;
if ( $post ) {
$modifier_type = 'id';
}
}
return $modifier_type;
}
function custom_rtmedia_action_query_modifier_value( $modifier_value, $raw_query ) {
global $is_album, $global_media_id;
$allowed_types = rtmedia_get_allowed_types();
$allowed_types = array_keys( $allowed_types );
if ( is_array( $raw_query ) && array_key_exists( '0' , $raw_query ) ) {
$post = custom_get_page_by_slug( $raw_query[0], OBJECT, 'attachment' );
$post_album = custom_get_page_by_slug( $raw_query[0], OBJECT, 'rtmedia_album,rtmedia_playlist', 'publish,hidden' );
$post = $post ? $post : $post_album;
if ( $post ) {
$modifier_value = rtmedia_id( $post->ID );
$is_album = is_rtmedia_album( $modifier_value );
$global_media_id = $modifier_value;
}
}
return $modifier_value;
}
function custom_rtmedia_pre_template() {
global $rtmedia_query, $rtmedia_interaction, $is_album, $global_media_id;
if ( isset( $rtmedia_query->media_query ) && array_key_exists( 'id' , $rtmedia_query->media_query ) ) {
$media_id = $rtmedia_query->media_query['id'];
$rtmedia_interaction->routes['media']->query_vars['0'] = $media_id;
} elseif ( $is_album ) {
$rtmedia_interaction->routes['media']->query_vars['0'] = 'album';
$rtmedia_interaction->routes['media']->query_vars['1'] = $global_media_id;
}
}
function custom_get_page_by_slug( $page_slug, $output = OBJECT, $post_type = 'page', $post_status = 'inherit' ) {
global $wpdb;
$post_type = str_replace( ',', '\',\'', $post_type );
$post_status = str_replace( ',', '\',\'', $post_status );
$page = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '$page_slug' AND post_type IN ( '$post_type' ) AND post_status IN ( '$post_status' )" );
if ( $page ) {
return get_post( $page, $output );
}
return null;
}
if ( class_exists( 'RTMedia' ) ) {
add_filter( 'get_rtmedia_permalink', 'custom_get_rtmedia_permalink', 10, 3 );
add_filter( 'rtmedia_action_query_modifier_type', 'custom_rtmedia_action_query_modifier_type', 10, 2 );
add_filter( 'rtmedia_action_query_modifier_value', 'custom_rtmedia_action_query_modifier_value', 10, 2 );
add_action( 'rtmedia_pre_template', 'custom_rtmedia_pre_template' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment