0

I've got a problem, what I'm trying to do is passing a url through a shortcode and use it as a background image, the problem is that the url gets processed and all the slashes gets removed. I tried removing the wordpress filter remove_filter( 'the_content', 'wpautop' );) but it doesn't seem to make any difference.

Here's my code:

function section_shortcode_func( $atts, $content = null ){
    $atts = shortcode_atts(
        [
        'bgimage' => '',
        ], $atts, 'section'
    );
    $return = '<div class="section" style="background-image: url("'. $atts['bgimage'] .'")">';  
}
  • How do you call the shortcode? Are you sure it doesn't get removed before the shortcode gets called (try str_replace("/","_", $atts["bgimage"]) to see if slashes were present)? – janh Oct 11 '17 at 8:17
  • [section bgimage="xxx.xxx/wp-content/uploads/2017/10/xxx.jpg"] I'm sure the link it's not filtered before the shortcode call because I did a vardump and the link it's fine. I just tried str_replace and yes it works. – Dave Oct 11 '17 at 8:20
  • the code you put in the question do nothing, edit your question to show the code you use – mmm Oct 11 '17 at 8:41
1

Here is the correct shortcode function

function section_shortcode_func( $atts, $content = null ){

    $atts       = shortcode_atts( array( 'bgimage' => '' ), $atts);
    $bgimage    = isset( $atts['bgimage'] ) ? $atts['bgimage'] : '';

    ob_start();
    echo "<div class='section' style='background-image: url($bgimage)'>";  
    return ob_get_clean();
}

Now you can call it this way

[section bgimage="xxx.xxx/wp-content/uploads/2017/10/xxx.jpg"] 
| improve this answer | |
  • @Dave If not worked this, then let me know, Thanks – Jaed Mosharraf Oct 11 '17 at 8:35
  • Jaed Mosharraf, a shortcode function must return is content and not echoing it – mmm Oct 11 '17 at 8:38
  • Ops My mistakes Correcting – Jaed Mosharraf Oct 11 '17 at 8:42
  • @mmm Now it's ok? – Jaed Mosharraf Oct 11 '17 at 8:43
  • You are always welcome :) – Jaed Mosharraf Oct 11 '17 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.