Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a piece of html code to run on every WordPress page

I have an interesting challenge that I'm coming up short on (I'm not a WordPress plugin developer). I am using a site that has OptimizePress as a theme. Optimizepress calls the_content many times in building a page.

I want to include a little code snippet in the body of every page.

if ( function_exists( "transposh_widget" ) ) {
    transposh_widget(array(), array(
        'title'       => 'Translation',
        'widget_file' => 'flags/tpw_flags_css.php',
    ) );
}

That shows a couple of flags and lets you switch languages on the site. So I need a div set to fixed position / 0,0 (I can adjust later for looks) that has that in it.

Other than outputting something in the_content, which seems to be the popular way to do this, what is the best way to achieve getting this on every page of a wordpress site?

Update

The code below started - here was my final code that output flags only in the top right corner of the site on every page.

<?php
/*
Plugin Name: Transposh Flags in Header
Description: Embed Transposh Header Code (in Footer of Page)
*/

function insert_my_footer() {
    echo '<div style="display:block; top:30px; right: 50px; position: fixed; color:white;">';
  if (function_exists("transposh_widget")) {
    transposh_widget(array(),
                     array('title'=>'',
                           'widget_file' => 'flags/tpw_flags.php') );
    }
    echo '</div>';
}

add_action('wp_footer', 'insert_my_footer');
?>
like image 795
4DMediaLab Avatar asked Nov 07 '13 20:11

4DMediaLab


1 Answers

OK, I'm not at all familiar with transposh_widget, so I don't really know where or when you want this code to run. However, it's easy to make a plugin that adds things to the bottom of a page, so perhaps you could try that.

Here's a basic plugin to get you started:

<?php
/*
Plugin Name: html in foot
Description: embed html at the foot of every page
*/

function insert_my_footer() {
  if (function_exists("transposh_widget")) {
    transposh_widget(array(),
                     array('title'=>'Translation',
                           'widget_file' => 'flags/tpw_flags_css.php') );
    }
    echo <<<END_SCRIPT
<!-- This will be inserted at the foot of every page -->
END_SCRIPT;
  }
}

add_action('wp_footer', 'insert_my_footer');
?>

To install this plugin, save it in your /wp-content/plugins directory (with appropriate ownership and permissions), and then activate it via the Wordpress admin pages.

like image 120
r3mainer Avatar answered Oct 12 '22 04:10

r3mainer