WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to write a simple smiley plugin with $wpsmiliestrans? :) (7 posts)

  1. pad
    Member
    Posted 5 years ago #

    Hi guys,

    I have my own set of smileys and at the moment, set them in functions.php, but everytime I upgrade wordpress I have to reupload them and edit the file and it's most tedious.

    I was wondering how to write a simple plugin based off this piece of documentation in the functions.php file:

    Plugins may override the default smiley list by setting the $wpsmiliestrans
    * to an array, with the key the code the blogger types in and the value the
    * image file.

    I would be ever so grateful for a response.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Like so:

    /*
    Plugin Name: Smiley replace
    */
    global $wpsmiliestrans;
    
    $wpsmiliestrans = array (
    ... stuff just like in the functions.php but with your changes ...
    );

    That's it.

  3. pad
    Member
    Posted 5 years ago #

    Thanks so much for this.

    Is there a way to add this bit to the plugin too, so I can change the directory of my smileys?

    $siteurl = get_option( 'siteurl' );
    	foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
    		$wp_smiliessearch[] = '/(\s|^)' . preg_quote( $smiley, '/' ) . '(\s|$)/';
    		$smiley_masked = attribute_escape( trim( $smiley ) );
    		$wp_smiliesreplace[] = " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' />

    Thanks a lot Otto!

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Well, if you want that too, then you'd do it slightly differently.

    Copy the entire smilies_init function into your plugin. Rename it to something else, like my_smilies_init and modify it however you like. Then add this code at the bottom of the plugin:

    remove_action('init', 'smilies_init', 5);
    add_action('init', 'my_smilies_init', 5);
  5. pad
    Member
    Posted 5 years ago #

    Thank you very much indeed.

    pad

  6. Monika
    Member
    Posted 5 years ago #

    Otto42
    wonderful :-)

    my first plugin and it works - without any error ;)

    thanks a lot!

    Monika

  7. Jannes
    Member
    Posted 5 years ago #

    I've created a custom smilies_init as well, but I have a small problem remaining.

    WP is kinda strict that there should be a space in front of and after the smiley. I would like to circumvent that. Is that possible?

    $siteurl = get_option( 'siteurl' );
    foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
    $wp_smiliessearch[] = '/(\s|^)' . preg_quote( $smiley, '/' ) . '(\s|$)/';
    $smiley_masked = attribute_escape( trim( $smiley ) );
    $wp_smiliesreplace[] = " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";

    I think I need to edit some line here, but I don't really understand what's happening here.

Topic Closed

This topic has been closed to new replies.

About this Topic