WordPress.org

Ready to get started?Download WordPress

Forums

Changing the form enctype dynamicly (2 posts)

  1. kurak
    Member
    Posted 4 years ago #

    Is there a way (by a plugin function) to change the form enctype (in new post form) to multipart/form-data

    The problem is that I don't want to edit the file always after upgrading the WP source.

  2. freido
    Member
    Posted 4 years ago #

    Hello Kurak,

    Here is a simple solution to your problem using jQuery:

    if ( is_admin() ) {
        function add_post_enctype() {
            echo "<script type='text/javascript'>
                      jQuery(document).ready(function(){
                          jQuery('#post').attr('enctype','multipart/form-data');
                      });
                  </script>";
        }
        add_action('admin_head', 'add_post_enctype');
    }

    If you want to test for a particular admin page for example "add page" or "edit-page" here is a slightly improved version of the same script:

    if ( is_admin() ) {
        $current_admin_page = substr( strrchr( $_SERVER['PHP_SELF'], '/' ),1, -4 );
        if ( $current_admin_page == 'page' || $current_admin_page == 'page-new' ) {
            function add_post_enctype() {
                echo "<script type='text/javascript'>
                          jQuery(document).ready(function(){
                              jQuery('#post').attr('enctype','multipart/form-data');
                      });
                      </script>";
            }
            add_action( 'admin_head', 'add_post_enctype' );
        }
    }

    Using this method you can add all sorts of attributes or html to the admin pages when and where needed.

Topic Closed

This topic has been closed to new replies.

About this Topic