WordPress.org

Ready to get started?Download WordPress

Forums

adding a custom text box to a theme via plugin (7 posts)

  1. razzetto and the happy people
    Member
    Posted 11 months ago #

    i'm trying to develop a very simple plugin to add a "shout" box in a theme, to get a less boring home page. Basically i have a custom post type called "shout box", that should let people to write some text and this text should appear just under the navigation menu, in home page. So when we launch the site this written could be "hey, this is our new site", but could easily changed to "check out our new projects".

    i managed it modifying a template inserting this:

    <div class='shoutbox'>
       <?php while($custom_shoutbox_posts->have_posts()) : $custom_shoutbox_posts->the_post() ?>
        <?php $custom_shoutbox_message= get_the_content();
    			print $custom_shoutbox_message; ?>
        <?php endwhile ?>
    </div>

    this is (quite) good.. but is there a way to add this to any theme? now, if i change theme, i must modify the new homepage template..
    the problem is that i don't know which function, action and hook to choose to make this more generale and less 'template-dependant'

    thanks

  2. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    Have you reviewed Writing_a_Plugin?

  3. razzetto and the happy people
    Member
    Posted 11 months ago #

    yes, but can't get how to add the code i pasted to any homepage template of any theme.

    perhaps could you point me to something more specific?

    thanks

  4. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

  5. razzetto and the happy people
    Member
    Posted 11 months ago #

    thanks. was in doubt about filter vs. action.
    i'll try to find the right hook here http://codex.wordpress.org/Plugin_API/Filter_Reference

  6. razzetto and the happy people
    Member
    Posted 11 months ago #

    what do you think about this approach:

    <?php
    if ( is_home() ) {
        add_filter( 'the_content', 'my_function_shouts_box' )
    }
    ?>

    on the right way?

    thanks

  7. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    No - not quite. Try something like:

    function my_the_content_filter($content) {
      if ( is_home() ) {
        $content = [insert markup etc];
      }
      return $content;
    }
    add_filter( 'the_content', 'my_the_content_filter' );

Reply

You must log in to post.

About this Topic