How to automatically change slugs to match titles for Soliloquy sliders

Posted on

For all post types in WordPress slugs are automatically generated based on their titles when entries are published. WordPress slug is the value of post_name field in wp_posts table. If you want to edit an entry’s slug to match its title when the title is changed, it can be done by editing the last string in the permalink, deleting the old one and hitting enter to have WordPress automatically generate the matching slug.

When using Soliloquy (free or paid) there’s no built-in option to change the slug by editing the permalink.

Slider Slug field available in Misc tab is not the same as WordPress slug. For example,

In  $slide_image_urls = soliloquy( 'front-page-header', 'slug', array(), true );

front-page-header is the Soliloquy-specific internal slug. This can be changed in Misc settings.

In $front_page_header_slider = get_page_by_path( 'front-page-header', OBJECT, 'soliloquy' );

front-page-header is the WordPress slug. Soliloquy does not provide a way to change this in the GUI.

By using wp_insert_post_data filter hook we can force WordPress slugs for Soliloquy sliders to automatically be updated so they match their corresponding titles. Add the following in child theme’s functions.php:

// Updates slug to match title for Soliloquy sliders
function sk_force_update_slug( $data, $postarr ) {

	if ( 'soliloquy' === $postarr['post_type'] && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
		$data['post_name'] = sanitize_title( $data['post_title'] );
	}

	return $data;

}
add_filter( 'wp_insert_post_data', 'sk_force_update_slug', 99, 2 );

Note: For existing sliders the code will not do anything unless the slider is re-saved/updated.