Modified your plugin to allow me to change og:title in the post.
-
I found not being able to customize the title unacceptable, so I’ve made some changes. I already shared this (an earlier version) in response to another post, but I thought I’d share the most recent version here using the “code” tag so it hopefully presents better.
First, the big reason why I wanted to be able to customize this information per post. I didn’t just want the title of a post being used as the og:title. I also wanted some other information to show up. For instance, instead of just “Poem Title” (without the quotes), have “Poem Title” — poem by Author (This time the title is inside quotes).
This is also important for other types of pages. It’s been frustrating that WP treats FB and SEO titles and descriptions the same. They’re just not even in the same ballpark. When your post shows up in a Google search, you really want to present an excerpt from the post. When it shows up at Facebook, you want a brief explanation of the post along with an invitation to come check it out.
Toward this end, I’m using Yaost SEO specifically for managing search engine related descriptions, titles, and keywords while I’m using your app specifically for Social Networking (especially Facebook) related descriptions, titles, and images. Your plugin was a.l.m.o.s.t there. Just need to be able to customize the titles.
Here’s the code I’ve added:
In the file “class-webdados-fb-open-graph-admin.php” I’ve added this to the function “post_meta_box” just before the code managing the description text area:
<!-- EAT -- Begin attempt to add custom title edit field --> <?php // Get current title $value_title = get_post_meta($post->ID, '_webdados_fb_open_graph_specific_title', true); ?> <p> <strong> <label for="webdados_fb_open_graph_specific_title"> <?php _e('Use this title', 'wonderm00ns-simple-facebook-open-graph-tags'); ?>: </label> </strong> <br/> <?php if ( $webdados_fb->is_yoast_seo_active() && $this->options['fb_show_wpseoyoast']==1 ) { _e('The Yoast SEO integration is active, so it\'s title will be used', 'wonderm00ns-simple-facebook-open-graph-tags'); } else { ?> <input type="text" id="webdados_fb_open_graph_specific_title" name="webdados_fb_open_graph_specific_title" value="<?php echo esc_attr( $value_title ); ?>" size="75"/> <br/> <?php _e('If this field is not filled, the title will be generated from the name you\'ve given your post or page', 'wonderm00ns-simple-facebook-open-graph-tags'); } ?> </p> <!-- EAT -- End attempt to add custom title edit field -->
In the same file, I’ve added the following to the function “save_meta_boxes” just after your code for processing the custom description inside the “if ($save) {” block:
// EAT begin attempt to add title data if ( isset($_POST['webdados_fb_open_graph_specific_title']) ) { // Sanitize user input. if ( get_bloginfo('version')>='4.7.0' ) { //sanitize_textarea_field only exists since 4.7.0 $mydata = trim( sanitize_textarea_field( $_POST['webdados_fb_open_graph_specific_title'] ) ); } else { //Just in case... $mydata = trim( sanitize_text_field( $_POST['webdados_fb_open_graph_specific_title'] ) ); } // Update the meta field in the database. update_post_meta($post_id, '_webdados_fb_open_graph_specific_title', $mydata); } // EAT End attempt to add title data
In the file “class-webdados-fb-open-graph-public.php” I’ve moved the “// begin/end original” code into the “else” end of an “if” statement as follows:
//Look for custom meta title. Use it if it's there. If not, use original code. if ( $fb_title = trim( get_post_meta($post->ID, '_webdados_fb_open_graph_specific_title', true) ) ) { //From our metabox } else { // begin original code $fb_title = wp_strip_all_tags( stripslashes( $post->post_title ), true ); //SubHeading if ( isset($this->options['fb_show_subheading']) && intval($this->options['fb_show_subheading'])==1 && $webdados_fb->is_subheading_plugin_active() ) { if (isset($this->options['fb_subheading_position']) && $this->options['fb_subheading_position']=='before' ) { $fb_title = trim( trim(get_the_subheading()).' - '.trim($fb_title), ' -' ); } else { $fb_title = trim( trim($fb_title).' - '.trim(get_the_subheading()), ' -' ); } } // end original code }
That’s it! It’s working fantastic. I’m going through all my posts now and using this new field to update the og:title as desired while at the same time further differentiating the SEO title to better optimize search engine results.
Hope you’ll use this in your next update.
- The topic ‘Modified your plugin to allow me to change og:title in the post.’ is closed to new replies.