How to access XFtoWP option and meta values
Table of Contents
- Access XFtoWP option
- Get user meta
- Get post meta
Use the following helper functions to access save data from the
xfwp option key, and even get usermeta and postmeta values from any post, page, or user with a connected item.
Integrate these functions into your own custom integrations to create your own syncing tools. You can manipulate any user and connected thread with access to native WordPress data structures.
Access XFtoWP option
To access all saved values from the WP admin > XenForo admin page options, access the following function which returns your site data in an array format:
$option = xf_option();
This accesses the
xfwp option row created in the
wp_options database table. You can also make calls to specific data by traversing the returned array like so:
$get_forums = xf_option( array( 'data', 'get_forums' ) );
The above function gets an array of the known forums you’ve added in WP admin > XenForo > Site connection > Forums.
Again, data called from this function is pre-cached (optimally saved to your database) so there is no performance hit to your site to access this data about your forum!
If a value is empty or does not exist, the function will safely return as empty. You can set a default value to any call like so:
$board_title = xf_option( array( 'settings', 'board_title' ), 'Default value' );
Get user meta
XFtoWP also stores custom user data to each synced user’s
wp_usermeta table and can easily be accessed with the following template function:
$wp_usermeta = xf_user_meta( $wp_user_id );
For this call to work, you must enter the ID of the user you wish to access.
To view an array of users synced to any WP user’s account, you can print the following:
$users = xf_user_meta( $wp_user_id, 'users' );
If a value you are trying to access is empty, the function will return an empty value. You can also set a default value if the return is empty by passing a third parameter:
$first_sync_date = xf_user_meta( $wp_user_id, 'first_sync', 'Never synced.' );
Get post meta
You can also access data saved to each post (when creating/connecting a thread) by accessing the
xfwp post meta key. This holds the list of posts and other thread info per post:
$wp_postmeta = xf_post_meta();
Traverse the postmeta data in the same way. Here’s how you get a list of the first page of posts saved to your post meta:
$thread_replies = xf_post_meta( array( 'posts' ) );
This function attempts to automatically grab the current post/page ID, but you can also set the post ID yourself by passing a second parameter:
$thread_replies = xf_post_meta( array( 'posts' ), 444 );
Lastly, any value accessed by this function will return an empty value if nothing is found. You can pass a default value to the third parmeter in that case if needed:
$thread_replies = xf_post_meta( array( 'posts' ), 444, 'Nothing found' );