最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Line breaks inside shortcode variable

programmeradmin2浏览0评论

So I made this shortcode for a client that creates an expandable transcription box. It works as intended except if there is a line break in the code. Then it create an extra p tag inside the div

function transciptor( $atts = '' ) {
    $value = shortcode_atts( array(
        'text' => '',
        'title' => 'Video transcript:'
    ), $atts );
    return '
    <style>
    #trans_title {
        margin: 0 auto;
        width: calc(60% + 48px);
        font-weight: bold;
    }
    #transcript_div > p {
        width: 60%;
        margin: 0 auto;
        max-height: 500px;
        overflow: scroll;
        padding: 24px;
        background-color: #e9f4fb;
        transition: 200ms;
    }
    #transcript_div > a {
        text-align: center;
        display: block;
        margin: 0 auto;
        width: fit-content;
        background-color: #252a2d;
        padding: 12px 36px;
        border-radius: 36px;
        position: relative;
        bottom: 24px;
        color: white;
    }
    </style>
    <p id="trans_title">'.$value['title'].'</p>
    <div id="transcript_div">
        <p>'.$value['text'].'</p>
        <a href="#">Expand</a>
    </div>
    <script>
        trans_text = document.querySelector("#transcript_div > p");
        trans_btn = document.querySelector("#transcript_div > a");
        expanded = false;

        trans_btn.onclick = function(event){
            event.preventDefault();
            expanded = !expanded;

            if(expanded == false){
                trans_text.style.maxHeight = "500px";
                trans_btn.innerHTML  = "Expand";
            }
            if(expanded == true){
                trans_text.style.maxHeight = "fit-content";
                trans_btn.innerHTML  = "Collapse";
            }
        }
    </script>

    ';
}
add_shortcode( 'transcript', 'transciptor' );

for example entering the code like this:

[transcript text="example line

example line"]

create two paragraph tags. How do I get the shortcode to add in linebreaks instead of making extra p tags?

UPDATED WITH $CONTENT

function transciptor( $atts = '', $content ) {
    $value = shortcode_atts( array(
        'title' => 'Video transcript:'
    ), $atts );
    return '
    <style>
    #trans_title {
        margin: 0 auto;
        width: calc(60% + 48px);
        font-weight: bold;
    }
    #transcript_div > p {
        width: 60%;
        margin: 0 auto;
        max-height: 500px;
        overflow: scroll;
        padding: 24px;
        background-color: #e9f4fb;
        transition: 200ms;
    }
    #transcript_div > a {
        text-align: center;
        display: block;
        margin: 0 auto;
        width: fit-content;
        background-color: #252a2d;
        padding: 12px 36px;
        border-radius: 36px;
        position: relative;
        bottom: 24px;
        color: white;
    }
    </style>
    <p id="trans_title">'.$value['title'].'</p>
    <div id="transcript_div">
        <p>'.$content.'</p>
        <a href="#">Expand</a>
    </div>
    <script>
        trans_text = document.querySelector("#transcript_div > p");
        trans_btn = document.querySelector("#transcript_div > a");
        expanded = false;

        trans_btn.onclick = function(event){
            event.preventDefault();
            expanded = !expanded;

            if(expanded == false){
                trans_text.style.maxHeight = "500px";
                trans_btn.innerHTML  = "Expand";
            }
            if(expanded == true){
                trans_text.style.maxHeight = "fit-content";
                trans_btn.innerHTML  = "Collapse";
            }
        }
    </script>

    ';
}
add_shortcode( 'transcript', 'transciptor' );

So I made this shortcode for a client that creates an expandable transcription box. It works as intended except if there is a line break in the code. Then it create an extra p tag inside the div

function transciptor( $atts = '' ) {
    $value = shortcode_atts( array(
        'text' => '',
        'title' => 'Video transcript:'
    ), $atts );
    return '
    <style>
    #trans_title {
        margin: 0 auto;
        width: calc(60% + 48px);
        font-weight: bold;
    }
    #transcript_div > p {
        width: 60%;
        margin: 0 auto;
        max-height: 500px;
        overflow: scroll;
        padding: 24px;
        background-color: #e9f4fb;
        transition: 200ms;
    }
    #transcript_div > a {
        text-align: center;
        display: block;
        margin: 0 auto;
        width: fit-content;
        background-color: #252a2d;
        padding: 12px 36px;
        border-radius: 36px;
        position: relative;
        bottom: 24px;
        color: white;
    }
    </style>
    <p id="trans_title">'.$value['title'].'</p>
    <div id="transcript_div">
        <p>'.$value['text'].'</p>
        <a href="#">Expand</a>
    </div>
    <script>
        trans_text = document.querySelector("#transcript_div > p");
        trans_btn = document.querySelector("#transcript_div > a");
        expanded = false;

        trans_btn.onclick = function(event){
            event.preventDefault();
            expanded = !expanded;

            if(expanded == false){
                trans_text.style.maxHeight = "500px";
                trans_btn.innerHTML  = "Expand";
            }
            if(expanded == true){
                trans_text.style.maxHeight = "fit-content";
                trans_btn.innerHTML  = "Collapse";
            }
        }
    </script>

    ';
}
add_shortcode( 'transcript', 'transciptor' );

for example entering the code like this:

[transcript text="example line

example line"]

create two paragraph tags. How do I get the shortcode to add in linebreaks instead of making extra p tags?

UPDATED WITH $CONTENT

function transciptor( $atts = '', $content ) {
    $value = shortcode_atts( array(
        'title' => 'Video transcript:'
    ), $atts );
    return '
    <style>
    #trans_title {
        margin: 0 auto;
        width: calc(60% + 48px);
        font-weight: bold;
    }
    #transcript_div > p {
        width: 60%;
        margin: 0 auto;
        max-height: 500px;
        overflow: scroll;
        padding: 24px;
        background-color: #e9f4fb;
        transition: 200ms;
    }
    #transcript_div > a {
        text-align: center;
        display: block;
        margin: 0 auto;
        width: fit-content;
        background-color: #252a2d;
        padding: 12px 36px;
        border-radius: 36px;
        position: relative;
        bottom: 24px;
        color: white;
    }
    </style>
    <p id="trans_title">'.$value['title'].'</p>
    <div id="transcript_div">
        <p>'.$content.'</p>
        <a href="#">Expand</a>
    </div>
    <script>
        trans_text = document.querySelector("#transcript_div > p");
        trans_btn = document.querySelector("#transcript_div > a");
        expanded = false;

        trans_btn.onclick = function(event){
            event.preventDefault();
            expanded = !expanded;

            if(expanded == false){
                trans_text.style.maxHeight = "500px";
                trans_btn.innerHTML  = "Expand";
            }
            if(expanded == true){
                trans_text.style.maxHeight = "fit-content";
                trans_btn.innerHTML  = "Collapse";
            }
        }
    </script>

    ';
}
add_shortcode( 'transcript', 'transciptor' );
Share Improve this question edited Apr 29, 2020 at 23:40 sallycakes asked Apr 29, 2020 at 22:52 sallycakessallycakes 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You could try using the enclosing form to write the shortcode and make use of the $content returned variable.

You need to declare you short code function with the extra parameter:

function transciptor( $atts = '', $content ) {

and then write your shortcode as:

[transcript]
place the value that
will be passed to
$content here
[/transcript]

Note the alternative syntax for the shortcode. There is information about it in the codex here.

If the p tags are still there I see two other options. Don't enclose your shortcode parameter in paragraph tags, i.e.

'.$value['text'].'

instead of

<p>'.$value['text'].'</p>

or you could parse the p tags from the variable with something like:

$value['text'] = str_replace("<p>", "", $value['text']);
$value['text'] = str_replace("</p>", "<br>", $value['text']);
发布评论

评论列表(0)

  1. 暂无评论