The $mod
variable is defined early in the WPSSO load process (in WpssoHead->show_head()
, which is hooked to the 'wp_head' action) and is passed to most WPSSO methods and filters.
$wpsso =& Wpsso::get_instance(); // Maybe prefer the global $post object. $use_post = apply_filters( 'wpsso_use_post', false ); // Get information about the current webpage. $mod = $wpsso->page->get_mod( $use_post );
The $mod
variable name stands for module and defines important reference values for the current WPSSO object type. The WPSSO object type can be a comment, post, term, or user. An archive does not have an object type since an archive page is a collection of object types. For example, a monthly archive is a collection of posts for that month. In this case, $mod[ 'obj' ]
would be false and other properties would be true, like $mod[ 'is_archive' ]
, $mod[ 'is_date' ]
, and $mod[ 'is_month' ]
.
The WpssoPage->get_mod()
method can be used to determine the current webpage module. If you need to setup a $mod
variable for a specific comment, post, term, or user, you can call the get_mod()
methods from those class objects directly.
$wpsso =& Wpsso::get_instance(); // Get information for comment ID 123. $mod = $wpsso->comment->get_mod( 123 ); // Get information for post ID 123. $mod = $wpsso->post->get_mod( 123 ); // Get information for term ID 123. $mod = $wpsso->term->get_mod( 123 ); // Get information for user ID 123. $mod = $wpsso->user->get_mod( 123 );
Here is a $mod
array for an example post:
Array ( [id] => 123 [name] => post [name_transl] => post [obj] => object WpssoPost [wp_obj] => object WP_Post [query_vars] => Array () [paged] => false [paged_total] => 1 [is_404] => false [is_archive] => false [is_comment] => false [is_date] => false [is_day] => false [is_home] => false [is_home_page] => false [is_home_posts] => false [is_month] => false [is_post] => true [is_post_type_archive] => false [is_public] => false [is_search] => false [is_term] => false [is_user] => false [is_year] => false [use_post] => false [post_slug] => the-post-slug [post_type] => post [post_type_label_plural] => Posts [post_type_label_single] => Post [post_mime] => '' [post_status] => publish [post_author] => 123 [post_coauthors] => Array () [post_time] => 2013-03-15T22:23:27+00:00 [post_modified_time] => 2021-01-31T00:16:46+00:00 [tax_slug] => '' [tax_label_plural] => false [tax_label_single] => false [user_name] => '' )
An example to retrieve custom post meta:
if ( $mod[ 'is_post' ] && $mod[ 'id' ] ) { $value = get_post_meta( $mod[ 'id' ], '_example_meta_name', $single = true ); }
The 'obj' element can be used to call object methods. Here's an example to get a custom Open Graph description value (if one has been defined):
$og_desc = $mod[ 'obj' ]->get_options( $mod[ 'id' ], 'og_desc' );
Function wrappers for the WPSSO get_mod()
methods are also available:
// Get information about the current webpage (post, term, user, archive page, etc.). $mod = wpsso_get_page_mod(); // Get information for a comment ID. $mod = wpsso_get_comment_mod( $comment_id ); // Get information for a post ID. $mod = wpsso_get_post_mod( $post_id ); // Get information for a term ID. $mod = wpsso_get_term_mod( $term_id ); // Get information for a user ID. $mod = wpsso_get_user_mod( $user_id );