• Resolved PeVo

    (@pevo)


    Hi there,

    I’m new to wordpress development (obviously) and am having issues trying to get a very simple plugin up and running. The idea is that it takes a url from an anchor and adds images to particular file types:

    <?
    /*
            Plugin Name: Add Document Type Styles
            Plugin URI: http://robospeare.com/wordpress/plugin_doctypes
            Description: Detects URLs in your post and page contet and applies style
            to those that link to documents so as to identify the document type. Support
            for .pdf and .mp3
            Version: 1.0
            Author: Oliver Sneyd
            Author URI: http://robospeare.com
    */
    
    function documenttypestyles_regex( $text )
    {
            $text = ereg_replace(
                    'href=([\'|"][:alnum:]|[:punct:]]*)
                            \.(pdf|mp3) ([\'|"])',
                    'herf=\\1.\\2\\3 class="link \\2"',
                    $text);
    
            return $text;
    }
    
    function documenttypestyles_styles()
    {
            echo "<style>";
            echo ".link { background-repeat : no-repeat; padding : 2px 0 2px 20px; }\n";
            echo ".pdf { background-image: url('" . WP_PLUGIN_URL . "/documenttypestyles/pdfIcon.jpg'); }\n";
            echo ".mp3 { background-image: url('" . WP_PLUGIN_URL . "/documenttypestyles/mp3Icon.gif'); }\n";
            echo "</style>\n\n";
    }
    
    add_filter( 'the_content', 'documenttypestyles_regex' );
    add_action( 'wp_head', 'documenttypestyles_styles' );
    ?>

    But if I have anything in the documenttypestyles_styles function the header for every page gets messed up – this is displayed at the top of every page:

    “; echo “.link { background-repeat : no-repeat; padding : 2px 0 2px 20px; }\n”; echo “.pdf { background-image: url(‘” . WP_PLUGIN_URL . “/documenttypestyles/pdfIcon.jpg’); }\n”; echo “.mp3 { background-image: url(‘” . WP_PLUGIN_URL . “/documenttypestyles/mp3Icon.gif’); }\n”; echo “\n\n”; } add_action( ‘wp_head’, ‘documenttypestyles_styles’ ); ?>

    Any ideas what I’m doing wrong here?

Viewing 4 replies - 1 through 4 (of 4 total)
  • It looks like it’s not interpreting the double quotes correctly. Make sure whatever you’re using to write the code isn’t saving those as smart-quotes (or whatever they’re called). Since there’s only a little actual php in that function, you could try bypassing the problem all together:

    function documenttypestyles_styles() {
    ?>
        <style type='text/css'>
        .link { background-repeat : no-repeat; padding : 2px 0 2px 20px; }
        .pdf { background-image: url('<?php echo WP_PLUGIN_URL; ?>/documenttypestyles/pdfIcon.jpg'); }
        .mp3 { background-image: url('<?php echo WP_PLUGIN_URL; ?>/documenttypestyles/mp3Icon.gif'); }
        </style>
    <?php
    }
    Thread Starter PeVo

    (@pevo)

    Fantastic – thanks!

    Thread Starter PeVo

    (@pevo)

    Actually what it looks like it was tripping up over was having

    <?

    all over the place instead of

    <?php

    Ah, didn’t even notice that. 🙂
    Yeah, some PHP set-ups don’t allow the short open tag. Always best to use <?php.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Basic plugin development help’ is closed to new replies.