Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:18:43+00:00 2026-06-18T04:18:43+00:00

I am making a plugin, to register a shortcode, which when used like this:

  • 0

I am making a plugin, to register a shortcode, which when used like this: [append_css mycss] it will look for a custom field called mycss and add the contents to the head of the document. It all works great, except that the code is being added to the body, and I don’t know how to get it to add to the head.

I have tried adding an action wp_head but I don’t know how to pass variables whilst doing that, and it does not seem to fire from within the shortcode callback anyway.

function append_css_short($params){
  global $post;
  if(sizeof($params)){
    $key = $params[0];
  } else {
    $key = 'css';
  }
  return '<style type="text/css">'.
  get_post_meta($post->ID, $key, true)
  .'</style>';
}
add_shortcode('append_css','append_css_short');

How would I get this to write to the head instead of body?
Is there a better approach to the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:18:44+00:00Added an answer on June 18, 2026 at 4:18 am

    If you want to print something to the head of the document you need use add_action with wp_head hook.

    A better approach might be to not use a shortcode at all. Shortcodes are intended to add content or modify content. Instead create a function that you can attach to wp_head that will print your css. You may want to force the user to keep the custom field name consistent. Or better yet have your plugin add a metabox to posts that they can enter custom css into. Then you will always be certain of the name of the field.

    function append_css() {
    
       global $post;
    
       if ( ! get_post_meta( $post->ID, 'mypluginname_css', true ) ) {
          return;
       }
    
       return '<style type="text/css">'. get_post_meta($post->ID, $key, true) .'</style>';
    
    }
    
    add_action( 'wp_head', 'append_css' );
    

    The only thing i am not certain of is whether or not $post with the custom field data will be available outside the loop. So you may have to add some extra querying in there to pull the custom data at the time that wp_head is fired.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a plugin which is called directly, from AJAX or similar. I'm
So my friend keeps making requests about this plugin I am writing for Bukkit
I'm making a plugin that does a custom query on the WordPress database, and
I'm working on making a plugin for Mail.app. I'd like the plugin to add
I am learning django-cms. I tried to make custom plugin which was quite successful
I am making a bukkit plugin, and I am using an API called MCStats,
I'm making a lightbox plugin called WowBox. Right now, if you want to make
I am making a wordpress plugin, and I used a query to get all
I'm making a plugin that triggers on the create message of a custom activity
I'm making a wordpress plugin. I've used add_query_string() inside anchors in order to load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.