How do i manually place a widget code

Is there a way that i can place a certain widget anywhere on the site?

For example:

Read More

I’m using the text widget in my sidebar, for the title I put YouTube, and I placed the YouTube embed code in it.
But I want that widget to go at the top of the site. How do I make this possible, while still using the widgets menu, but not show up in the sidebar, but wherever I place it?

If it’s not possible, does anyone know of a YouTube sidebar widget, one that can let you place the widget code anywhere on the site and also do it from the widgets menu?

Related posts

Leave a Reply

2 comments

  1. Just register another widget area. Don’t take the name sidebar literally, you can use the fields anywhere on your page.

    In your theme’s functions.php:

    add_action( 'after_setup_theme', 'register_multiple_widget_areas' );
    
    function register_multiple_widget_areas()
    {
        register_sidebar(
            array(
            'name'          => 'Sidebar',
            'id'            => 'sidebar',
            'description'   => 'describe the function of the box.'
            )
        );
        register_sidebar(
            array(
            'name'          => 'Header',
            'id'            => 'header-widget',
            'description'   => 'Goes at the top of the page.'
            )
        );
    }
    

    And wherever you need the header widget call:

    dynamic_sidebar( 'header-widget' );
    

    See also register_sidebar() and dynamic_sidebar().

  2. You can use the_widget():

    the_widget(
        'WP_Widget_Archives',
        [
            'title'    => __( 'Archives', 'your-textdomain' ),
            'count'    => true,
            'dropdown' => true,
        ],
        [
            'before_widget' => '<section class="widget__container">',
            'after_widget'  => '</section>',
            'before_title'  => '<h2 class="widget__title">',
            'after_title'   => '</h2>',
        ]
    );