Sanitize Post Title in MB View

Support MB Views Sanitize Post Title in MB View

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #39231
    Guillaume LORAINGuillaume LORAIN
    Participant

    Hello.
    I would like to use the post title as image alt-text in a view.
    Is it possible to use a sanitized version of the title to be sure that it will not break my code because I use a lot of quotation marks in my titles.

    Thanks.

    #39238
    PeterPeter
    Moderator

    Hello there,

    When inserting a post, the post title is sanitized by default, the HTML tags are stripped. You can read more on the WordPress documentation https://developer.wordpress.org/reference/functions/wp_insert_post/#security
    The quotation mark will not break the code itself if you store it in a variable.

    #39261
    Guillaume LORAINGuillaume LORAIN
    Participant

    Here is the code I use in my view :
    {% set field = attribute( post, 'photo-q' ) %}
    img src="{{ field.medium_large.url }}" width="{{ field.medium_large.width }}" height="{{ field.medium_large.height }}" title="{{ post.title }}"

    My post title is : What means "N" in the word NASA ?

    The output brocken code is :
    img src="/20221022215105-1225212.jpg" width="768" height="384" title="What means "N" in the word NASA"

    I will try to use wp_strip_all_tags to see if it can fix it.

    #39262
    Guillaume LORAINGuillaume LORAIN
    Participant

    wp_strip_all_tags didn't help me but now I use mb.str_replace('"', "", post.title) so now it's working as i want. I don't know if this code is the best but it's working.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.