Sure. In your single.php
or where you loop through the posts, start by getting the content:
$content = get_the_content();
Then use a regex where you target anything within <h2>
tags:
$regex = '/<h2>(.*?)\<\/h2>/u';
And then use preg_match_all()
to find all titles:
preg_match_all($regex, $content, $table_of_content);
This will create a multidimensional array, see:
print_r($table_of_content);
So you have the titles stored in $table_of_content[0]
.
Using this array, and a variation of the regex to get the id or data-attribute which I would go with, you’re set to go.
Related Posts:
- How to include PHP files in plugins the correct way
- function deactivate_plugins does not exist
- What is an alternative method to the WordPress private _doing_it_wrong() function
- Using OOP plugin’s methods throughout the website
- function triggered by “manage_users_custom_column” filter not working
- How can I change the frequency of a scheduled event?
- WordPress plugin how to run function when button is clicked
- Shortcodes, output buffering, and WordPress functions
- How can i list current author’s categories?
- Determine which theme location a wp_get_nav_menu_items is for
- When to check if a function exists
- Checking for existence of a page by title?
- Dynamically Override Fancy Title
- Problem with `wp_mail()`
- Calling a function from functions.php in custom page/ blog post
- Check if I am in the Admin Panel (wp-admin)?
- Call to undefined function get_blog_option()
- What is the difference between current_page_parent and current_page_ancestor?
- Plugin development: is adding empty index.php files necessary?
- Undefined function wp_set_password
- Does add_filter work outside functions.php
- What function to hook for changes made in status and visibility of a post
- how to use preg_replace for wordpress video?
- How can I add a CSS rule to edit.php?
- wp_enqueue_script + wp_enqueue_style Since When
- Widget update function not saving values
- $content variable – Is this a reserved variable for a WordPress function? – php / wordpress
- How to use WP_Term with (menu) argument in the wp_nav_menu?
- WordPress after content Hook & external template part
- Enqueue scripts in footer
- override pluggable.php functions
- Retrieve contents/page names of various pages for use in a plugin
- Can wp_list_comments output into variable?
- is user logged in not working
- Can I change header.php of current theme through a plugin function?
- get_current_screen() return null
- Override class inside $atts shortcode_atts in WordPress
- creating custom function to log actions in plugin
- add_rewrite_rule works in themes function php but when moved into plugin it stops working
- Maximum lifetime for nonce
- Adding option to Gallery shortcode
- correct way to call javascript into hook function
- How to change the hover content of a specific menu item on WordPress?
- How do I update a field of a meta box?
- How to add user details to different tables immediately after user registration
- WordPress function get_the_terms() returns ‘Invalid taxonomy’ error
- Checking for existence of a page by title?
- how to get context information inside my funcion
- WordPress class, using add_action to call member function does not work
- wp_get_theme Warning: Illegal offset type
- Why wp_die() doesn’t work with wp_redirect but exit() works
- Warning: call_user_func_array() expects parameter 1 to be a valid callback
- Print Dashboard submenu name and filename
- Alternative functions for mysql_free_result and mysql_ping in wordpress functions
- WordPress multisite,use same cookies across all website?
- Apply styles to blockquote element with the WYSIWYG editor
- Call to undefined function is_home() or any conditional tags
- unregister_setting() vs delete_option() for plugin update
- How to change WooCommerce loop product title HTML output in single product page and archive page
- How to output functions from plugin inside theme
- wp_schedule_event is registered but function isn’t running
- Remove Permalink Meta Box not working?
- Setting post_id for single.php based on URL without a redirect
- using admin functions on frontend
- Javascript Function Called Too Early in PHP Script
- Modify code for functions.php with specific twitter user url and hashtags
- can’t access some WordPress function from my plugin
- WP 3-way voting system: On to something! Please help!
- Custom Settings Plugin Save foreach checkboxes
- Customise Grouped Product display in Woocommerce with custom column
- Change the behaviour of a button
- Can someone explain what’s the use of parse request function in WordPress?
- Add default value from selection
- function add custom fields to media gallery
- Override category archive page title (not the head title)
- Make visible page only in the trash
- Redirection from a specific page for users logged in but not with membership
- WordPress: I wanted to use Add rewrite rule multiple times for multiple CPT in my plugin
- Why would one specify multiple arguments when using get_previous_posts_link() which takes only one parameter?
- I cant add is_admin control to plugin
- WordPress Ajax not returning Response
- Remove tag from all posts when publishing new post
- wp_ajax add_action fuction won’t fire on custom jQuery action
- Add function to Dokan ajax class
- foreach argument to get specific file types getting too many returns
- Remove from a div by class name from post page if post author role is not administrator
- Function not working on any file other than the main plugin file
- Building WordPress Themes With Bootstrap with Adi Purdila → TutsPlus
- Ajax not working to insert, query and result data
- Template file structure , wordpress hook for altering the template
- How can I see a varibles value when my plugin runs?
- How can I add recent posts to menu like mashable
- Why is ‘register_activation_hook’ undefined?
- Dynamic banner for use on external sites
- Custom options page for themes
- Why does this fail: Disabling plugins enqueue_script() in functions.php
- Call do_action() within WordPress cronjob
- How to plugin function code move to theme function.php ? I’ve tried below code but not working [closed]
- WordPress simple ‘Hello World’ plugin – problems
- get_term_by() returning null on plugin