{"id":5148,"date":"2024-08-09T08:07:45","date_gmt":"2024-08-09T08:07:45","guid":{"rendered":"https:\/\/isophal.com\/?p=5148"},"modified":"2024-08-09T08:09:01","modified_gmt":"2024-08-09T08:09:01","slug":"to-create-a-wordpress-plugin-that-automatically-adds-the-necessary-open-graph-meta-tags","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2024\/08\/09\/5148.html\/","title":{"rendered":"To create a WordPress plugin that automatically adds the necessary Open Graph meta tags"},"content":{"rendered":"\n<p>when a post is shared on Facebook, you can follow these steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Plugin File<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to your WordPress installation directory.<\/li>\n\n\n\n<li>Navigate to <code>wp-content\/plugins\/<\/code>.<\/li>\n\n\n\n<li>Create a new directory for your plugin, e.g., <code>og-meta-tags<\/code>.<\/li>\n\n\n\n<li>Inside this directory, create a file named <code>og-meta-tags.php<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Add Plugin Information and Basic Setup<\/h3>\n\n\n\n<p>Open the <code>og-meta-tags.php<\/code> file and add the following code:<\/p>\n\n\n\n<p>Here&#8217;s the complete <code>og-meta-tags.php<\/code> file with the plugin information, settings page, and basic setup included:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n * Plugin Name: OG Meta Tags for Facebook Sharing\n * Description: Automatically adds Open Graph meta tags to WordPress posts for better sharing on Facebook. Includes settings for Facebook App ID and Secret.\n * Version: 1.0\n * Author: Professor Web Developer\n *\/\n\n\/\/ Add a settings menu\nfunction og_meta_tags_menu() {\n    add_options_page(\n        'OG Meta Tags Settings', \n        'OG Meta Tags', \n        'manage_options', \n        'og-meta-tags-settings', \n        'og_meta_tags_settings_page'\n    );\n}\nadd_action('admin_menu', 'og_meta_tags_menu');\n\n\/\/ Display the settings page\nfunction og_meta_tags_settings_page() {\n    ?>\n    &lt;div class=\"wrap\">\n        &lt;h1>OG Meta Tags Settings&lt;\/h1>\n        &lt;form method=\"post\" action=\"options.php\">\n            &lt;?php\n            settings_fields('og_meta_tags_settings');\n            do_settings_sections('og-meta-tags-settings');\n            submit_button();\n            ?>\n        &lt;\/form>\n    &lt;\/div>\n    &lt;?php\n}\n\n\/\/ Register settings\nfunction og_meta_tags_settings_init() {\n    register_setting('og_meta_tags_settings', 'og_meta_tags_app_id');\n    register_setting('og_meta_tags_settings', 'og_meta_tags_app_secret');\n\n    add_settings_section(\n        'og_meta_tags_section',\n        'Facebook App Settings',\n        'og_meta_tags_section_callback',\n        'og-meta-tags-settings'\n    );\n\n    add_settings_field(\n        'og_meta_tags_app_id',\n        'Facebook App ID',\n        'og_meta_tags_app_id_callback',\n        'og-meta-tags-settings',\n        'og_meta_tags_section'\n    );\n\n    add_settings_field(\n        'og_meta_tags_app_secret',\n        'Facebook App Secret',\n        'og_meta_tags_app_secret_callback',\n        'og-meta-tags-settings',\n        'og_meta_tags_section'\n    );\n}\nadd_action('admin_init', 'og_meta_tags_settings_init');\n\nfunction og_meta_tags_section_callback() {\n    echo 'Enter your Facebook App credentials:';\n}\n\nfunction og_meta_tags_app_id_callback() {\n    $app_id = get_option('og_meta_tags_app_id');\n    echo \"&lt;input type='text' name='og_meta_tags_app_id' value='\" . esc_attr($app_id) . \"' \/>\";\n}\n\nfunction og_meta_tags_app_secret_callback() {\n    $app_secret = get_option('og_meta_tags_app_secret');\n    echo \"&lt;input type='text' name='og_meta_tags_app_secret' value='\" . esc_attr($app_secret) . \"' \/>\";\n}\n\n\/\/ Retrieve Facebook App credentials\nfunction get_facebook_app_credentials() {\n    $app_id = get_option('og_meta_tags_app_id');\n    $app_secret = get_option('og_meta_tags_app_secret');\n\n    return &#91;\n        'app_id' => $app_id,\n        'app_secret' => $app_secret,\n    ];\n}\n\n\/\/ Add Open Graph meta tags to the head section of each post\nfunction add_og_meta_tags() {\n    if (is_single()) {\n        global $post;\n        $post_thumbnail = get_the_post_thumbnail_url($post->ID, 'full');\n        $post_description = strip_tags($post->post_excerpt);\n        if (empty($post_description)) {\n            $post_description = wp_trim_words($post->post_content, 30, '...');\n        }\n        ?>\n        &lt;meta property=\"og:type\" content=\"article\" \/>\n        &lt;meta property=\"og:title\" content=\"&lt;?php echo esc_attr(get_the_title()); ?>\" \/>\n        &lt;meta property=\"og:description\" content=\"&lt;?php echo esc_attr($post_description); ?>\" \/>\n        &lt;meta property=\"og:image\" content=\"&lt;?php echo esc_url($post_thumbnail); ?>\" \/>\n        &lt;meta property=\"og:url\" content=\"&lt;?php echo esc_url(get_permalink()); ?>\" \/>\n        &lt;?php\n    }\n}\nadd_action('wp_head', 'add_og_meta_tags');\n\n\/\/ Use Facebook credentials (Example of usage with SDK)\nfunction post_to_facebook($link, $message) {\n    $credentials = get_facebook_app_credentials();\n    \n    \/\/ Example usage: Create Facebook instance using the credentials\n    \/\/ Replace '{access-token}' with the actual access token when using the SDK\n    $fb = new Facebook\\Facebook(&#91;\n        'app_id' => $credentials&#91;'app_id'],\n        'app_secret' => $credentials&#91;'app_secret'],\n        'default_graph_version' => 'v10.0',\n    ]);\n\n    try {\n        $response = $fb->post('\/me\/feed', &#91;\n            'link' => $link,\n            'message' => $message,\n        ], '{access-token}');\n        return $response->getGraphNode();\n    } catch (Facebook\\Exceptions\\FacebookResponseException $e) {\n        echo 'Graph returned an error: ' . $e->getMessage();\n        exit;\n    } catch (Facebook\\Exceptions\\FacebookSDKException $e) {\n        echo 'Facebook SDK returned an error: ' . $e->getMessage();\n        exit;\n    }\n}\n?>\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Activate the Plugin<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the WordPress admin dashboard.<\/li>\n\n\n\n<li>Navigate to <code>Plugins<\/code> > <code>Installed Plugins<\/code>.<\/li>\n\n\n\n<li>Find the &#8220;OG Meta Tags for Facebook Sharing&#8221; plugin and click <code>Activate<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>is_single()<\/strong>: Checks if the current page is a single post.<\/li>\n\n\n\n<li><strong>get_the_post_thumbnail_url()<\/strong>: Retrieves the URL of the featured image for the post.<\/li>\n\n\n\n<li><strong>strip_tags()<\/strong>: Removes any HTML tags from the post excerpt.<\/li>\n\n\n\n<li><strong>wp_trim_words()<\/strong>: Trims the post content to 30 words if there is no excerpt.<\/li>\n\n\n\n<li><strong>esc_attr() and esc_url()<\/strong>: Ensures that the data is safely outputted in the meta tags.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Customization:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can modify the number of words in <code>wp_trim_words()<\/code> if you need a shorter or longer description.<\/li>\n\n\n\n<li>You can customize the <code>og:type<\/code> or any other tags if your website has specific requirements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Summary:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Plugin Setup:<\/strong> This plugin adds Open Graph meta tags to each post, which helps ensure that when your posts are shared on Facebook, the correct information (title, description, image, etc.) is displayed.<\/li>\n\n\n\n<li><strong>Settings Page:<\/strong> The plugin also includes a settings page where you can securely enter your Facebook App ID and App Secret.<\/li>\n\n\n\n<li><strong>SDK Integration (Optional):<\/strong> The code includes an example of how you might integrate the Facebook SDK to post content directly to Facebook.<\/li>\n<\/ul>\n\n\n\n<p>This complete file can be saved as <code>og-meta-tags.php<\/code> in your WordPress plugin directory, and then you can activate the plugin through the WordPress admin panel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>when a post is shared on Facebook,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[583],"tags":[],"class_list":["post-5148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=5148"}],"version-history":[{"count":2,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5148\/revisions"}],"predecessor-version":[{"id":5151,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5148\/revisions\/5151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/5149"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=5148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=5148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=5148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}