Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Plugin to include snippet of code into selected parts of the layout

Trying to find some confirmation on how this is possible within the confines of a plugin without success:

The goal is for a user to use a plugin that would insert a pre-defined snippet of code in a selected part of the layout. A typical use would be:

  • User selects "Below header"
  • Included code appears below header

  • User selects "Above footer"

  • Included code appears above footer

The "code" snippet would be something like a set of links, or an image. So that they could select from a drop-down menu, where it should appear on their site without editing any theme code.

I suppose it would also be useful to provide a tag that devs can paste somewhere in their theme template to be included somewhere more complicated, something like .

like image 330
Kobius Avatar asked Jan 25 '16 06:01

Kobius


People also ask

How do I add a code to snippets?

With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root. Double-click it. The snippet code is inserted in the code file.


2 Answers

In Wordpress, you can modify the output of the system in particular places, by using action hooks and filters. For instance:

function putSomeTextInTheFooter() {
    echo '<p>This text is inserted at the bottom</p>';
}
add_action( 'wp_footer', 'putSomeTextInTheFooter' );

This PHP code, would modify the footer by adding a custom action [function], which Wordpress would fire right at the event called "wp_footer", which is self explanatory. There are hooks and filters for all kinds of things. Wordpress has a rich feature set for modifying just about everything. This kind of code can go in either a theme, or a plugin. Check out the codex here: https://codex.wordpress.org/Plugin_API/Action_Reference

like image 132
Jim Maguire Avatar answered Oct 21 '22 13:10

Jim Maguire


Write a shortcode function in function.php

<?php 
     function test()
    {
      echo "I am test ";
    }

    add_shortcode("test_shortcode","test"); ?>

Now you can add [test_shortcode] any where in you editor to get the result there

Or in the template you can call it with

<?php    echo  do_shortcode([test_shortcode]); ?>

Hope this will help you to solve your problem

like image 39
Vikas Gautam Avatar answered Oct 21 '22 12:10

Vikas Gautam