1

FINAL UPDATE: Updated code from @birgire solved my issue. If you are facing a similar issue, please read the accepted answer.


ORIGINAL QUESTION IN DETAIL: Currently under the appearance menu in admin, there is a link called 'editor', which when clicked leads to editable main-stylesheet, my effort is to put a link to a custom-stylesheet instead, because it is more convenient and I could save me a lot of time in the long run. This link may appear as a sub-menu item under appearance (direct link to my custom-stylesheet.css), which I tend to click more often than anything else.

In effort to achieve that I followed the instructions given in this article on how to add menu items in wp and I was able to construct a code below.

add_action('admin_menu', 'add_appearance_menu');
          function add_appearance_menu() {
add_submenu_page( 'themes.php', 'Custom Stylesheet', 'customstyle', 'manage_options', 'custom-style.css', '$function'); }

However, the code is still not complete as you can see I had no clue what to insert in the actual function in line 3. If you must know, the custom-stylesheet.css is in the root of theme folder and also that I am using a premium theme, but I am not sure if this is a theme level issue.

This incomplete code still seems to work well otherwise, as I can see an added menu under the appearance menu. So, It does not seem absurd, what I am trying to achieve. Although it throws an invalid function error when I click the created menu link. Please give me a hint as to where I should be looking for this function.

3 Answers 3

2

You can check out this excellent answer by @Eugene Manuilov. In your case the relevant admin page action is:

load-appearance_page_customstyle

and the url to the custom stylesheet you want to edit:

get_admin_url().'theme-editor.php?file=custom-stylesheet.css&theme='. get_stylesheet().'&scrollto=0';

Then your code example would be:

add_action('admin_menu', 'add_appearance_menu');    
function add_appearance_menu() {
    add_submenu_page( 'themes.php', 'Custom Stylesheet', 'customstyle', 'manage_options', 'customstyle', '__return_null'); 
}

add_action( 'load-appearance_page_customstyle', 'custom_redirect' );
function custom_redirect() {
    if ( 'customstyle' === filter_input( INPUT_GET, 'page' ) ) {
        $file2edit = "custom-stylesheet.css"; // change this to your needs
        $location = get_admin_url().'theme-editor.php?file='.$file2edit.'&theme='. get_stylesheet().'&scrollto=0';
        wp_redirect( $location, 301);
        exit();
    }
}

When you click the submenu link

http://example.com/wp-admin/themes.php?page=customstyle

customstyle

the hook load-appearance_page_customstyle from

do_action('load-' . $page_hook);

in /wp-admin/admin.php is activated with the redirect defined above.

5
  • 1
    The provided code works fine but it directly opens the css file in the browser hence rendering it uneditable, while I was expecting it to open within the admin and would be editable just like any other file.
    – gurung
    Commented Apr 29, 2013 at 5:55
  • 1
    And also I failed to understand why the function argument is left as 'dummy'. Kindly elaborate if you may.
    – gurung
    Commented Apr 29, 2013 at 5:58
  • 1
    I misunderstood you ;-) I updated the code to open the custom stylesheet in the Wordpress file editor. By 'dummy' function I just meant that you could use a function that does nothing. I guess we can just use the WordPress function __return_null in that case.
    – birgire
    Commented Apr 29, 2013 at 7:48
  • @bigire , thanks, you have been very helpful. Just curious, what would change if I need the same link for my custom-functions.php.
    – gurung
    Commented Apr 29, 2013 at 9:18
  • 1
    sure, I updated the code so you can change $file2edit to your needs.
    – birgire
    Commented Apr 29, 2013 at 9:24
1

From the documentation:

$function

(callback) (optional) The function to be called to output the content for this page.

In other words, this is the function that will be responsible for outputting content on your submenu page. You need to create it, give it a nice unique name, and then specify this name as the last argument into add_submenu_page:

public function wp2804_submenu_output_function {
    ?>
    <h1>here be the title</h1>
    <?php
} // end of wp2804_submenu_output_function 
add_submenu_page( 'themes.php', /* all the other stuff */ 'wp2804_submenu_output_function');
1
  • thnks for the pointer, however now I am stuck with the warning: cannot modify headers :(
    – gurung
    Commented Apr 29, 2013 at 7:33
-1

You can try to insert css menu widgets with submenu on it.You can download some decent widgets from

http://freebietemplate.com/css_designs/css_designs.html

1
  • unfortunately your answer is completely irrelevant.
    – gurung
    Commented Apr 29, 2013 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.