Published on 11/12/2020
Published by amit
Showing correct page title when using Awesome Apps and Custom Post for Yoast SEO
Recently we needed to create a subsite within an Awesome App, and we had to show the other posts using app module, something like <site url>/<app>/ebook/<post-slug>. Currently, in this case, awesome enterprise platform is not able to automatically show the correct post title.
To fix this put the following code in the site-specific plugin,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
add_filter( 'wpseo_opengraph_title', 'wpdl_seo_opengraph_title' ); function wpdl_seo_opengraph_title( $title ) { global $post; if($post->post_type == 'p_content' && $post->post_name == 'ebook'){ $qs= aw2_library::get('qs'); if(empty($qs)) return $title; // this is main page //since qs is not empty we need to get the post title and change it. $tpost = get_page_by_path( $qs[0], OBJECT, 'c_ebook' ); //util::var_dump($qs); // util::var_dump($tpost); $string = WPSEO_Meta::get_value( 'title', $tpost->ID ); if ($string !== '') { $replacer = new WPSEO_Replace_Vars(); $title = $replacer->replace( $string, $tpost ); } return $title; } return $title; } add_filter( 'wpseo_opengraph_url', 'wpdl_seo_opengraph_url' ); function wpdl_seo_opengraph_url( $url ) { global $post; if($post->post_type == 'p_content' && $post->post_name == 'ebook'){ $qs= aw2_library::get('qs'); if(empty($qs)) return $title; // this is main page //since qs is not empty we need to get the post title and and change it. $tpost = get_page_by_path( $qs[0], OBJECT, 'c_ebook' ); $url = get_post_permalink( $tpost ); return $url; } return $url; } add_action( 'wpseo_add_opengraph_images', 'wpdl_seo_add_images' ); function wpdl_seo_add_images( $object ) { global $post; if($post->post_type == 'p_content' && $post->post_name == 'ebook'){ $qs= aw2_library::get('qs'); if(empty($qs)) return; //since qs is not empty we need to get the post title and and change it. $tpost = get_page_by_path( $qs[0], OBJECT, 'c_ebook' ); $image = WPSEO_Meta::get_value( 'opengraph-image', $tpost->ID ); $object->add_image( $image ); } } add_filter('wpseo_title', 'wpdl_seo_filter_wpseo_title'); function wpdl_seo_filter_wpseo_title($title) { global $post; if($post->post_type == 'p_content' && $post->post_name == 'ebook'){ $qs= aw2_library::get('qs'); if(!empty($qs)){ $tpost = get_page_by_path( $qs[0], OBJECT, 'c_ebook' ); $string = WPSEO_Meta::get_value( 'title', $tpost->ID ); if ($string !== '') { $replacer = new WPSEO_Replace_Vars(); $title = $replacer->replace( $string, $tpost ); } } return $title; } } |
In the above code, you will have to replace the following to make it work
- p_content with the post type of your app page.
- ebook with the correct slug of the page which will be used
- c_ebook with the correct post type of the post whose slug is used after ‘ebook‘ page slug in the example.