Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Evan Fraser
Evan Fraser
6,789 Points

How to make a function that out puts a div inside the_content right below the opening <body> tag?

How to make a function that out puts a div inside the_content on the very top?

I have a plugin with a settings page and am wanting certain things inside the wordpress site to display when the fields are filled inside the settings page.

So for example, my global settings page has a google analytics ID field, when its entered, the google analytics tracking code will display right above the closing </head> tag using the wp_head, and that works totally fine.

My next problem is that I am trying to do this in the_content, but the div i am wanting to appear is not in the right spot, even though you can see that i set add_action( 'the_content', 'hawp_header_schema_opening_div', 1 ); with a priority of 1.

I am very new to this I just need help with what im doing wrong.

<?php
/**
 * Plugin Name: Hawp Media Developer Settings
 * Plugin URI: https://hawpmedia.com
 * Description: This plugin implements additional search engine optimization into the backend of the site.
 * Version: 1.0.0
 * Author: Hawp Media
 * Author URI: https://hawpmedia.com
 * License: GPL2
 */

/*----------------------------------------------------------------------------
 * Hawp Media Global Settings Page
 * ---------------------------------------------------------------------------*/
// The page content surrounding the settings fields
function hawp_global_settings_page(){
  ?>
  <div class="wrap">
    <h1>Hawp Media Settings</h1>
    <p>This information is used around the website, so changing these here will update them across the website.</p>
    <form method="post" action="options.php">
      <?php
      settings_fields("company_information");
      do_settings_sections("hawp-settings");
      submit_button();
      ?>
    </form>
  </div>
  <?php 
}

// Here are the settings fields to display you can use inputs, textareas, checkboxes and multi-selects
function display_hawp_opt_logo(){ ?>
  <input type="url" name="hawp_opt_logo" placeholder="Enter LOGO URL" value="<?php echo get_option('hawp_opt_logo'); ?>" size="65">
<?php }
function display_hawp_opt_branding(){ ?>
  <input type="text" name="hawp_opt_branding" placeholder="Enter BRANDING" value="<?php echo get_option('hawp_opt_branding'); ?>" size="65">
<?php }
function display_hawp_opt_phone(){ ?>
  <input type="tel" name="hawp_opt_phone" placeholder="Enter PHONE #" value="<?php echo get_option('hawp_opt_phone'); ?>" size="65">
<?php }
function display_hawp_opt_keyphrase(){ ?>
  <input type="text" name="hawp_opt_keyphrase" placeholder="Enter PRIMARY KEYPHRASE" value="<?php echo get_option('hawp_opt_keyphrase'); ?>" size="65">
<?php }
function display_hawp_opt_street(){ ?>
  <input type="text" name="hawp_opt_street" placeholder="Enter STREET ADDRESS" value="<?php echo get_option('hawp_opt_street'); ?>" size="65">
<?php }
function display_hawp_opt_city(){ ?>
  <input type="text" name="hawp_opt_city" placeholder="Enter CITY" value="<?php echo get_option('hawp_opt_city'); ?>" size="65">
<?php }
function display_hawp_opt_state(){ ?>
  <input type="text" name="hawp_opt_state" placeholder="Enter 2 digit STATE CODE" value="<?php echo get_option('hawp_opt_state'); ?>" size="65">
<?php }
function display_hawp_opt_zip(){ ?>
  <input type="text" name="hawp_opt_zip" placeholder="Enter ZIP" value="<?php echo get_option('hawp_opt_zip'); ?>" size="65">
<?php }
function display_hawp_opt_globalcta(){ ?>
  <textarea type="text" name="hawp_opt_globalcta" placeholder="Enter GLOBAL CTA (also used for social sharing message)" value="<?php echo get_option('hawp_opt_globalcta'); ?>" cols="65" rows="3"><?php echo get_option('hawp_opt_globalcta'); ?></textarea>
<?php }
function display_hawp_opt_bodyschema(){ ?>
  <input type="text" name="hawp_opt_bodyschema" placeholder="SCHEMA TYPE IE LocalBusiness" value="<?php echo get_option('hawp_opt_bodyschema'); ?>" size="65">
<?php }
function display_hawp_opt_productontology(){ ?>
  <input type="text" name="hawp_opt_productontology" placeholder="PRODUCTONTOLOGY PAGE Not Full URL" value="<?php echo get_option('hawp_opt_productontology'); ?>" size="65">
<?php }
function display_hawp_opt_analytics(){ ?>
  <input type="text" name="hawp_opt_analytics" placeholder="ANALYTICS UA CODE Not Full script" value="<?php echo get_option('hawp_opt_analytics'); ?>" size="65">
<?php }

// This tells WP what to enqueue into the <form> area. You need:  1. add_settings_section  2. add_settings_field  3. register_setting
function display_custom_info_fields(){
  add_settings_section("company_information", "Company Information", null, "hawp-settings");
  add_settings_field("hawp_opt_logo", "Company Logo URL", "display_hawp_opt_logo", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_branding", "Business Name", "display_hawp_opt_branding", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_phone", "Primary Phone", "display_hawp_opt_phone", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_keyphrase", "Primary Keyphrase", "display_hawp_opt_keyphrase", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_street", "Primary Street Address", "display_hawp_opt_street", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_city", "Primary City", "display_hawp_opt_city", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_state", "Primary State", "display_hawp_opt_state", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_zip", "Primary Zip", "display_hawp_opt_zip", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_globalcta", "Global/Share CTA", "display_hawp_opt_globalcta", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_bodyschema", "Body Tag Schema", "display_hawp_opt_bodyschema", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_productontology", "Productontology Page", "display_hawp_opt_productontology", "hawp-settings", "company_information");
  add_settings_field("hawp_opt_analytics", "Analytics UA Code", "display_hawp_opt_analytics", "hawp-settings", "company_information");
  register_setting("company_information", "hawp_opt_logo");
  register_setting("company_information", "hawp_opt_branding");
  register_setting("company_information", "hawp_opt_phone");
  register_setting("company_information", "hawp_opt_keyphrase");
  register_setting("company_information", "hawp_opt_street");
  register_setting("company_information", "hawp_opt_city");
  register_setting("company_information", "hawp_opt_state");
  register_setting("company_information", "hawp_opt_zip");
  register_setting("company_information", "hawp_opt_globalcta");
  register_setting("company_information", "hawp_opt_bodyschema");
  register_setting("company_information", "hawp_opt_productontology");
  register_setting("company_information", "hawp_opt_analytics");
}
add_action("admin_init", "display_custom_info_fields");

// This adds the settings page to the WordPress admin and sets the url slug
function hawp_settings_menu(){
  add_menu_page( 'Hawp Settings', 'Hawp Settings', 'manage_options', 'hawp_global_settings', 'hawp_global_settings_page', 'http://dev.hawpmedia.com/hawpbetheme/wp-content/themes/betheme-child/images/admin-ico.jpg', '60');
}
add_action('admin_menu', 'hawp_settings_menu');


/*----------------------------------------------------------------------------
 * Google Analytics Tracking Code
 *
 * If hawp_opt_analytics ID is filled in the settings page, the Google
 * analytics code will display with the analytics ID you entered.
 * ---------------------------------------------------------------------------*/
if ( get_option('hawp_opt_analytics') ) {
    function hawp_media_google_analytics() {
    ?>
        <!-- Google Analytics Code Open -->
        <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

        ga('create', '<?php echo get_option('hawp_opt_analytics'); ?>', 'auto');
        ga('send', 'pageview');
        </script>
        <!-- Google Analytics Code Close -->
    <?php
    }
    add_action( 'wp_head', 'hawp_media_google_analytics', 99999 );
}


/*----------------------------------------------------------------------------
 * Header Schema Div
 * 
 * If hawp_opt_bodyschema is filled in the settings page, the Schema div will
 * display & if the hawp_opt_productontology is filled in the settings page,
 * the link tag will display under the schema div in the top of the_content
 * ---------------------------------------------------------------------------*/

// Displays opening schema div on the top of the_content
if ( get_option('hawp_opt_bodyschema') ) {
    function hawp_header_schema_opening_div() {
    ?>
        <!-- Body Schema Open -->
        <div id="<?php echo get_option('hawp_opt_bodyschema'); ?>" itemscope itemtype="http://schema.org/<?php echo get_option('hawp_opt_bodyschema'); ?>" itemref="<?php echo preg_replace('/\s+/', '-',get_option('hawp_opt_city')); ?>-Location">
        <?php
        if(get_option('hawp_opt_productontology')) {
          echo '<link id="more-specific" itemprop="additionalType" href="http://www.productontology.org/id/'. get_option('hawp_opt_productontology') .'">';
        }
        ?>
        <!-- Body Schema Close -->
    <?php
    }
    add_action( 'the_content', 'hawp_header_schema_opening_div', 1 );
}

// Displays closing schema div
if ( get_option('hawp_opt_bodyschema') ) {
    function hawp_header_schema_closing_div() {
    ?>
        </div><!-- Body Schema Closing Div -->
    <?php
    }
    add_action( 'the_content', 'hawp_header_schema_closing_div', 99999 );
}