• Hello,

    I would like that every time a word appears, it is styled differently than the rest of the text – automatically ??

    Is it possible?

Viewing 10 replies - 1 through 10 (of 10 total)
  • you can try to create a filter function for 'the_content' to wrap that word into a span with CSS class for formatting;

    example code to be added into functions.php of your (child) theme:

    add_filter( 'the_content', 'auto_word_style' );
    
    function auto_word_style( $text ) {
    $text = str_replace( 'specialword', '<span class="auto-style">specialword</span>', $text );
    return $text;
    }

    and add for example this to the styles (in style.css of a child theme or via a custom CSS plugin):

    .auto-style { ...whatever formatting you want... }

    Thread Starter vinnie2k

    (@vinnie2k)

    Thank you for the idea.

    Just so that I understand you well:
    – the_content is the PHP page(s) where the filter will be applied
    – auto_word_style is the function
    – specialword is the word I want to style differently
    – auto_style is the CSS style that defines how I want to style the word

    Am I right?

    'the_content' refers to the content of the page or post, what you type into the editor, not the PHP code or any other structure of the page.

    if you only want to apply it to pages, you need to adapt the code slightly.

    in the example, the CSS class is .auto-style (with hyphen, not under line);

    apart from that, you have got it right.

    review also
    https://codex.www.ads-software.com/Function_Reference/add_filter
    https://codex.www.ads-software.com/Plugin_API/Filter_Reference/the_content

    Thread Starter vinnie2k

    (@vinnie2k)

    I thought the_content was some dummy variable you inserted into the function as an example. My bad ??

    Now that I understand it, I’ll try it out.

    Thanks ??

    Thread Starter vinnie2k

    (@vinnie2k)

    Having issues getting the child theme to work, will get back to you when it does and I can safely implement the proposed changes.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What part are you struggling with?

    Thread Starter vinnie2k

    (@vinnie2k)

    Child theme not taking up the parent settings:
    https://themegrill.com/tutorial-on-creating-wordpress-child-theme/#comment-18746 (see second page of thread).

    Is there a manual way to do this?

    Thread Starter vinnie2k

    (@vinnie2k)

    BTW I’ve done the suggested changes in the child theme; only waiting for the child theme to work properly ??

    Thread Starter vinnie2k

    (@vinnie2k)

    OK so I modified the original functions.php file just to see if the function works.

    It doesn’t ??

    The text style in standard text (content) is not changed; however, image captions are all screwed up, most likely because of nested HTML tags?

    Help ??

    Thread Starter vinnie2k

    (@vinnie2k)

    For reference:

    add_filter( ‘the_content’, ‘auto_word_style’ );
    function auto_word_style( $text ) {
    $text = str_replace( ‘COAPTA’, ‘<span class=”auto-style”>COAPTA</span>’, $text );
    return $text;
    }

    and

    .auto-style {
    color: #F59D24;
    font-style: bolder;
    }

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Automatically change a word style’ is closed to new replies.