API and Filters
-
Hi I am using the WP API to publish articles to my website with WordPress.
I want to know if I can automatically update the Meta Description and Title within TSF when I publish through my API?
Thanks!
-
Hello!
Do you wish to populate a custom meta description or title? If not, TSF should automatically populate those for you, provided the content is stored in text or HTML.
Please try it out and let me know if you’re running into things and how I can reproduce them.
The title auto populates, which is great. I’m inserting a meta description that is not within the main content, like so:
post_data = {
‘title’: clean_meta_title,
‘status’: ‘draft’,
‘meta’: { ‘description’: clean_meta_description, }
}
So I’d like the ‘description’ to feed into the TSF Meta Description, is that possible?
Thanks.
-
This reply was modified 1 year, 8 months ago by
alphaexcapital.
Hello!
I understand. Yes, you can feed your content to TSF’s description generator.
See if this works, where
get_my_content()
should be replaced with a call to get your content:add_filter( 'the_seo_framework_fetched_description_excerpt', /** * @param string $excerpt The excerpt to use. * @param int $page_id Deprecated. * @param array|null $args The query arguments. Contains 'id', 'taxonomy', and 'pta'. * Is null when the query is auto-determined. */ function( $excerpt, $deprecated, $args ) { if ( ! $excerpt ) { // You could forward $args, which looks like: [ 'id' => 42, 'taxonomy' => 'category' ] $excerpt = get_my_content(); } return $excerpt; }, 10, 3 );
Note that this filter runs on any request where a post is visible, which is also in the admin area. This may cause issues if your methods do not support such complexity. If you know exactly how your content is stored, share that with me so I can try to give you pointers.
Hello, thanks for taking the time to work this out for me.
So, maybe this helps me understand, TSF pulls the autodescription (Meta Data) from the excerpt? If that is true, can I just post it the content I want to excerpt and it will be auto-populated?
Maybe that simplifies things?
Thanks
Sorry to double post, but is there a way to post directly to your SEO Meta Title section too?
I’d like to use this code found on your github where you add dynamic years to the title. Would it be possible to do this too?
So I’d have my normal title: ABC Keyword
then it will insert a different title within your Meta Title section: ABC Keyword – UPDATED 2023
If that makes sense?
Hi again!
Can I just post it the content I want to excerpt and it will be auto-populated?
Yes, that’s what the code I gave accomplishes, provided you change
get_my_content()
with a method that fetches your content. I don’t know how you store this content, so I cannot give additional instructions.Then it will insert a different title within your Meta Title section: ABC Keyword – UPDATED 2023
That’s best achieved if you manually input the year in the title. See line 3 of the gist. I recently wrote about this very issue on another topic:
Please be mindful that if your content doesn’t match the generated title, search engines may penalize your site (e.g., the title says “2023,” but the content says “2021,” and nothing has changed in 2 years, then the title is misleading).
That said, the code you found on GitHub still works. With it, in the custom meta title fields, you can add years by writing
%%year%%
, months by writing%%month%%
, and days by writing%%day%%
.Hey, thanks so much for the help. It’s all working. I really appreciate the support, I’ve always switched back to TSF after using other SEO plugins.
As for the month and date updates, I appreciate your feedback re: Google titles. I tried your solution and it works when you manually input the %%year%% into the meta title box, but when you try and use it for the Sitewide / Site Title feature, it doesn’t work as intended. I just wanted a global feature. Is it possible or not with the code you provided?
Hello!
Then you may want to use shortcodes instead.
function current_year_shortcode() { return gmdate( 'Y' ); } add_shortcode( 'current_year', 'current_year_shortcode' );
Next to that snippet, you’ll also need to enable shortcode rendering for the titles, both in the theme and TSF:
add_filter( 'the_title', 'apply_shortcodes' ); add_filter( 'the_seo_framework_title_from_custom_field', 'apply_shortcodes' ); add_filter( 'the_seo_framework_title_from_generation', 'apply_shortcodes' );
With those two snippets combined, you can put
[current_year]
almost anywhere on your site to output the current year.-
This reply was modified 1 year, 7 months ago by
Sybre Waaijer. Reason: more details
Hey, thanks for the code.
I tried it but the output just prints the shortcode.
https://ibb.co/VM7YNHx This is where I set the shortcode.
https://ibb.co/p3fqMR6 This was the output.
I also noticed that the output schema puts this out as the Site Title too, so maybe I’ll avoid this method.
Is there a way to auto append the %%year%% to the tile at output level instead of trying the Site Title input in the settings?
Hi again!
Have you implemented both snippets? It’s two-part; here’s them together:
function current_year_shortcode() { return gmdate( 'Y' ); } add_shortcode( 'current_year', 'current_year_shortcode' ); add_filter( 'the_title', 'apply_shortcodes' ); add_filter( 'the_seo_framework_title_from_custom_field', 'apply_shortcodes' ); add_filter( 'the_seo_framework_title_from_generation', 'apply_shortcodes' );
Thanks for your time, I appreciate it. The code works but I’m looking to have the automated year added to the end of each title through the SEO settings, like so:
https://ibb.co/fQrYGjrThe shortcode doesn’t work within this box. Equally, it changes the Site Title in the schema, so it’s not ideal. If there is a way to happen the shortcode to the end of the title (or even just %%year%%) like the Site Title option does, that would be fantastic. If not, don’t worry – I don’t want to take too much of your time up :).
Hi again!
Now I see how you’re implementing this, I think we’re using the wrong tool for the job.
What exactly do you want your titles to be, what were they before you implemented the shortcode, and on which pages/archives do you want this to happen?
-
This reply was modified 1 year, 8 months ago by
- The topic ‘API and Filters’ is closed to new replies.