-
-
Notifications
You must be signed in to change notification settings - Fork 42
Use the blog collection in your functions (and filters)
lloc edited this page Feb 11, 2013
·
7 revisions
If you want to use the collection of blogs - which created the plugin - in your functions (and filters) you could write code like this:
function my_print_something() {
foreach ( MslsBlogCollection::instance()->get() as $blog ) {
printf(
'<link rel="alternate" hreflang="%1$s" href="http://%1$s.example.com/" />',
$blog->get_language()
);
}
}
add_action( 'wp_head', 'my_print_something' );
The above example prints the link references to your blogs in all html headers of the frontend output.
In this thread a user asked for a more customised version. And this is what I would do:
function my_print_something() {
$blogs = MslsBlogCollection::instance();
$mydata = MslsOptions::create();
foreach ( $blogs->get_objects() as $blog ) {
$language = $blog->get_language();
if ( $blog->userblog_id == $blogs->get_current_blog_id() ) {
$url = $mydata->get_current_link();
}
else {
switch_to_blog( $blog->userblog_id );
if ( 'MslsOptions' != get_class( $mydata ) && !$mydata->has_value( $language ) ) {
restore_current_blog();
continue;
}
$url = $mydata->get_permalink( $language );
restore_current_blog();
}
$language = substr( $language, 0, 2 );
printf(
'<link rel="alternate" hreflang="%s" href="%s" />',
( 'us' == $language ? 'en' : $language ),
$url
);
}
}
add_action( 'wp_head', 'my_print_something' );