How to get post title in WordPress?
Mastering WordPress Data: How to Retrieve Custom Titles from SEO Plugins As a senior developer, I often encounter scenarios where we need to extract specific...
Mastering WordPress Data: How to Retrieve Custom Titles from SEO Plugins
As a senior developer, I often encounter scenarios where we need to extract specific data from a WordPress installation, especially when third-party plugins are involved. The challenge you've described—needing the title generated by an SEO plugin (like All in one SEO Pack) rather than the default WordPress post title—is a very common requirement.
You have correctly identified that simply using get_the_title() will only retrieve the standard post title, ignoring the optimized title managed by your SEO plugin. To solve this, we need to shift our focus from the default post object to accessing the custom meta data stored by the plugin in the WordPress database.
This post will guide you through the developer-centric approach to retrieving the specific title required for your translation script, ensuring you get the exact text needed for conversion.
Understanding WordPress Data Retrieval
In WordPress, most plugin-specific information, such as SEO titles, meta descriptions, or custom settings, is stored in the wp_postmeta table. To access this data, we must use WordPress's built-in functions designed for retrieving post metadata, specifically get_post_meta().
When a plugin saves a value to the database, it typically associates that value with the post ID. We need to first ensure we are targeting the correct Post ID and then fetch the specific meta key used by the SEO pack.
Modifying Your Conversion Function
Your existing function for Arabic-to-English conversion is solid; the logic using preg_replace is effective for handling complex character mapping. The adjustment needed is in the data source within the function itself. Instead of relying on standard theme functions, we will use WordPress APIs to pull the SEO title.
Here is how you can refactor your code to fetch the title saved by the All in one SEO Pack plugin:
function arb2en_title($post_id)
{
// 1. Define the meta key used by AIOSEO (you must verify this key)
$seo_title_key = '_yoast_wpseo_title'; // This is a common Yoast SEO key, check your specific plugin documentation!
// 2. Retrieve the specific title from post meta using the Post ID
if ( ! $post_id ) {
return '';
}
$text = get_post_meta($post_id, $seo_title_key, true);
// Check if we successfully retrieved a title
if ( empty($text) ) {
return '';
}
// 3. Perform the Arabic to English conversion logic
$arb_en_map = array(
'د' => ']',
'ج' => '[',
'ح' => 'p',
'خ' => 'o',
'ه' => 'i',
'ع' => 'u',
'غ' => 'y',
'ف' => 't',
'ق' => 'r',
'ث' => 'e',
'ص' => 'w',
'ض' => 'q',
'ش' => 'a',
'س' => 's',
'ي' => 'd',
'ب' => 'f',
'ل' => 'g',
'ا' => 'h',
'ت' => 'j',
'ن' => 'k',
'م' => 'l',
'ك' => ';',
'ط' => '\'',
'ظ' => '/',
'ز' => '.',
'و' => ',',
'ة' => 'm',
'ى' => 'n',
'لا' => 'b',
'ر' => 'v',
'ؤ' => 'c',
'ء' => 'x',
'ئ' => 'z',
'إ' => 'Y',
'لإ' => 'T',
'لأ' => 'G',
'أ' => 'H',
'لآ' => 'B',
'آ' => 'N'
);
foreach($arb_en_map as $key => $value)
{
$text = preg_replace("/$key/", $value, $text);
}
return htmlentities($text);
}
Key Takeaways for Robust Development
Notice the crucial change: instead of passing a post number (or relying on $post), the function now requires the $post_id and uses get_post_meta(). This is the standard, robust way to interact with custom plugin data in WordPress. When building complex systems, like those you might encounter when architecting solutions similar to what is discussed in modern frameworks like Laravel, relying on well-defined data access methods rather than direct string manipulation ensures your code remains maintainable and scalable.
Conclusion
To successfully get the title from a specific SEO plugin within WordPress, you must bypass standard theme functions and delve into the database structure using get_post_meta(). By correctly identifying the unique meta key used by the All in one SEO Pack—and ensuring your function operates on the correct Post ID—you can reliably extract the data needed for your Arabic-to-English conversion. This approach ensures that your custom scripts interact cleanly with the dynamic content managed by your plugins, leading to accurate and reliable results every time.
Stefan
SEO engineer and Laravel developer. Building tools to help Laravel applications rank higher in search results.