Support » Plugin: REST API Post Embeds » retrieving wmpl posts

  • Resolved slotty7130

    (@slotty7130)


    hi,

    first thank you for that great plugin^^ i have a question for a wmpl feature

    i use your plugin on a site with 3 languages, which are managed by wmpl
    i retrieve the posts from a subdomain (a blog site) also with 3 languages managed by wmpl
    i use the shortcode in a sidebar widget (1 post order by random)…now i want to retrieve the posts dependendly from the active language (when my active language is englisch, then i want to retrieve the english blog posts)

    i can set different widgets on different languages but the problem is the request url for the languages

    so i installed also this plugin
    https://wordpress.org/plugins/wp-rest-api-multilanguage-over-wmpl/

    i am able to retrieve the language dependant posts from the blog site…but i have to ad “?lang=en/es/de/…” to the end of the api url

    now my question..is it possible to insert this string parameter at the end of the request url without to touch the core files?

    a great future shordcode attribute would be a blank container for a string in order to add it at the end of the request url

    any help would be great

    cheers
    martin

    https://wordpress.org/plugins/rest-api-post-embeds/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    I’m not really familiar with WPML, so I’m not sure I’ll be able to help.

    However, it looks like the plugin you installed allows you to add a specific WP API query parameter, lang, to get posts in a specific language. If that plugin works well, you could add a new lang parameter to my shortcode by adding the following to your theme’s functions.php, or to a functionality plugin:

    REST API Post Embeds: alter API query based on custom shortcode parameter

    Thread Starter slotty7130

    (@slotty7130)

    hi jeremy,

    thank you very much for that fast response and possible solution^^

    but i can`t get it to work

    i added your code into my functions.php and added the shortcode into my page
    [jeherve_post_embed url=”http://ipblog.directdesign-ffm.de/” wpapi=”true” Β number=”1″ lang=”en”]

    i hope i did it the right way

    here the url of the stage site with the feed…it shows the german post instead of the english
    http://industrial.directdesign-ffm.de/en/

    there are 3 german and 1 english posts on that draft blogsite

    when i call
    http://ipblog.directdesign-ffm.de/wp-json/wp/v2/posts

    i get the 3 german posts as json

    when i call
    http://ipblog.directdesign-ffm.de/wp-json/wp/v2/posts?lang=en

    i get only the one english post (as wanted)

    what might be wrong?

    cheers
    martin

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Thanks for the examples. I think it could be because the plugin doesn’t use the same standard as the core query parameters. I’ve updated the code snippet in my post to take that into account. Let me know how it goes.

    Thread Starter slotty7130

    (@slotty7130)

    YOU`RE A GENIUS!!

    works great^^
    thank you very much

    cheers
    martin

    Thread Starter slotty7130

    (@slotty7130)

    now that i can retrieve the posts from the proper language..i would like to define also the right language “source:” link at the bottom

    i looked at your filter code and difined a new attribute

    function jeherve_rest_api_embeds_urlsource( $out, $pairs, $atts, $shortcode ) {

    if ( isset( $atts[‘source’] ) && ” != $atts[‘source’] ) {
    $out[‘source’] = $atts[‘source’];
    }

    return $out;
    }

    add_filter( ‘shortcode_atts_jeherve_post_embed’, ‘jeherve_rest_api_embeds_urlsource’, 10, 4 );

    it works but i had to modify the core code:( as i don`t know how to achieve this by hooking

    i added also a target=”_blank” to the link…as the site owner wants to open all external links in a blank window

    $credits .= sprintf(
    _x( ‘Source: %1$s‘, ‘Site URL’, ‘rest-api-post-embeds’ ),
    $atts[‘source’]
    );

    it would be also a great feature to have the target of the permalinks as attributes… only as a feature suggestion^^

    your plugin is great

    cheers
    martin

    Thread Starter slotty7130

    (@slotty7130)

    works great with

    function jeherve_rest_api_embeds_target( $out, $pairs, $atts, $shortcode ) {

    if ( isset( $atts[‘target’] ) && ” != $atts[‘target’] ) {
    $out[‘target’] = $atts[‘target’];
    }

    return $out;
    }

    add_filter( ‘shortcode_atts_jeherve_post_embed’, ‘jeherve_rest_api_embeds_target’, 10, 4 );

    and

    $credits .= sprintf(
    _x( ‘Source: %1$s‘, ‘Site URL’, ‘rest-api-post-embeds’ ),
    $atts[‘source’]
    );

    could be a nice additional option to take the 2 new parameters into the core^^

    cheers

    Thread Starter slotty7130

    (@slotty7130)

    ups..the code is cutted..hehe…i set the target to target=”‘.$atts[‘target’].

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Instead of creating new shortcode attributes, I’d suggest the following:

    1. Remove the existing credits thanks to the existing include_credits attribute, like so:
      [jeherve_post_embed url="http://ipblog.directdesign-ffm.de/" wpapi="true" number="1" lang="en" include_credits="false"]
    2. Create a new function, hooked into the jeherve_post_embed_post_loop, to output a custom credit line including the language parameter, and with links opening in a new window, like so:
      /**
       * Custom credits HTML output.
       *
       * @param string  $loop            Post loop.
       * @param array   $posts_info      Array of data about our posts.
       * @param int     $number_of_posts Number of posts we want to display.
       * @param array   $atts            Array of shortcode attributes.
       *
       * @return string $str             Credits HTML output.
       */
      function jeherve_new_credits( $loop, $posts_info, $number_of_posts, $atts ) {
      	if ( isset( $atts['url'], $atts['lang'] ) ) {
      		$credits = '<div class="jeherve-post-embeds-credits">';
      		$credits .= sprintf(
      			_x( 'Source: <a target="_blank" class="jeherve-post-embeds-credit-link" href="http://%1$s/%2$s">%1$s/%2$s</a>', 'Site URL', 'rest-api-post-embeds' ),
      			$atts['url'],
      			$atts['lang']
      		);
      		$credits .= '</div>';
      		return $loop . $credits;
      	}
      	return $loop;
      }
      add_filter( 'jeherve_post_embed_post_loop', 'jeherve_new_credits', 98, 4 );
    Thread Starter slotty7130

    (@slotty7130)

    ah, that`s cool…works:)

    but i forgot to mention that i have to open all blog permalinks in a blank window….also the custom post links…so i did the same with the title and featured image in the core code

    '<h4 class="post-embed-post-title"><a href="%1$s" target="'.$atts['target'].'">%2$s</a></h4>'
    			'<div class="post-embed-post-thumbnail"><a title="%1$s" href="%2$s" target="'.$atts['target'].'"><img src="%3$s" alt="%1$s"/></a></div>'

    therefore i thought it would be great to have a target attribute…maybe there`s also a cool hook solution for that

    but thank you very much for your help^^

    cheers
    martin

    Thread Starter slotty7130

    (@slotty7130)

    i found a little bug in your last solution

    my main blog domain (in german) is http://ipblog.directdesign-ffm.de (without language add)

    your hook works only if there`s a lang attribute

    if i ad an lang=”de” to the shortcode then the source link gets http://ipblog.directdesign-ffm.de/de , which doesn`t work anymore (404)
    removing include_credits=”false” and lang shows only “source:”

    maybe less work for you to modify the core files with the 2 features: attributes for target and custom credit link^^..i mean, just now it`s something that i need..but i can imagine that these features could be also intersting for other users^^

    cheers
    martin

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    therefore i thought it would be great to have a target attribute

    I would rather not add that option. I don’t believe changing the browser’s default behaviour and open links in a new window is the right thing to do for the users here. You can read more about this here:
    https://css-tricks.com/use-target_blank/

    However, if you still would like to add target blank links to specific links on your site, you can use jQuery. Here is a quick example:
    https://gist.github.com/allybee/5871749

    if i ad an lang=”de” to the shortcode then the source link gets http://ipblog.directdesign-ffm.de/de , which doesn’t work anymore (404)

    To solve this issue, you can update the jeherve_new_credits() function I posted above, and change the value of $atts['lang'] to an empty string if its original value was de:

    if ( 'de' === $atts['lang'] ) {
        $atts['lang'] = '';
    }
    Thread Starter slotty7130

    (@slotty7130)

    thank you:))

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘retrieving wmpl posts’ is closed to new replies.