0

My Script creates an CSS File when i save the post with the shortcode ID at the end of the filename. e.g. (test-293.css) The Problem is, that i need to register this individual File in my frontend and i need the shortcodes ID in the add_action hook.How can i get this ID in add_action function load_front_scripts?

add_action('save_post','arti_carousel_save'); //Save Metabox CODE 
function arti_carousel_save( $post_id )
{
  cssfile($post_id); //create the File and transmit the shortcodes ID
}


function cssfile($id) //The Script to create the file -> it works fine
{
    $post_id = get_the_ID();
    require_once( ABSPATH . 'wp-admin/includes/file.php' ); // you have to load this file
    global $wp_filesystem;
    $upload_dir = wp_upload_dir();
    WP_Filesystem(); // Initial WP file system
    $dirCSS = trailingslashit($upload_dir['basedir']) . 'arti-carousel/';
    $cssCode = 'body{background-color:#000; !important}';
    $wp_filesystem->mkdir($dirCSS); // Make a new directory folder if folder not their
    $wp_filesystem->put_contents($dirCSS . 'arti-carousel-post-' . $id . '.css', $cssCode, 0644); // Finally, store the file :D
}

//The include of the Style to Frontend -> i cant get the shortcodes ID
//Its not work... ?
add_action('wp_enqueue_scripts','load_front_scripts');
function load_front_scripts($postid){
    $uploads = wp_upload_dir();
wp_register_style('arti_carousel_post_front_css',trailingslashit($uploads['baseurl']) . 'arti-carousel/arti-carousel-post-'.$postid.'.css', __FILE__);
wp_enqueue_style('arti_carousel_post_front_css');
}



// Register Custom Post Type
function arti_carousel_add_post_type() {

    $labels = array(
        'name'                  => _x( 'Carousels', 'Post Type General Name', 'arti-carousel' ),
        'singular_name'         => _x( 'Carousel', 'Post Type Singular Name', 'arti-carousel' ),
        'menu_name'             => __( 'Arti-Carousels', 'arti-carousel' ),
        'name_admin_bar'        => __( 'Arti-Carousels', 'arti-carousel' ),
        'archives'              => __( 'Item Archives', 'arti-carousel' ),
        'attributes'            => __( 'Item Attributes', 'arti-carousel' ),
        'parent_item_colon'     => __( 'Parent Item:', 'arti-carousel' ),
        'all_items'             => __( 'All Items', 'arti-carousel' ),
        'add_new_item'          => __( 'Add New Item', 'arti-carousel' ),
        'add_new'               => __( 'Add New', 'arti-carousel' ),
        'new_item'              => __( 'New Item', 'arti-carousel' ),
        'edit_item'             => __( 'Edit Item', 'arti-carousel' ),
        'update_item'           => __( 'Update Item', 'arti-carousel' ),
        'view_item'             => __( 'View Item', 'arti-carousel' ),
        'view_items'            => __( 'View Items', 'arti-carousel' ),
        'search_items'          => __( 'Search Item', 'arti-carousel' ),
        'not_found'             => __( 'Not found', 'arti-carousel' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'arti-carousel' ),
        'featured_image'        => __( 'Featured Image', 'arti-carousel' ),
        'set_featured_image'    => __( 'Set featured image', 'arti-carousel' ),
        'remove_featured_image' => __( 'Remove featured image', 'arti-carousel' ),
        'use_featured_image'    => __( 'Use as featured image', 'arti-carousel' ),
        'insert_into_item'      => __( 'Insert into item', 'arti-carousel' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'arti-carousel' ),
        'items_list'            => __( 'Items list', 'arti-carousel' ),
        'items_list_navigation' => __( 'Items list navigation', 'arti-carousel' ),
        'filter_items_list'     => __( 'Filter items list', 'arti-carousel' ),
    );
    $args = array(
        'label'                 => __( 'Carousel', 'arti-carousel' ),
        'description'           => __( 'Post Type Description', 'arti-carousel' ),
        'labels'                => $labels,
        'supports'              => false,
        'hierarchical'          => false,
        'supports'              => array( 'title' ),
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
    );
    register_post_type( 'arti_carousel', $args );

}

add_action( 'init', 'arti_carousel_add_post_type', 0 );
//Show Metabox
add_action( 'add_meta_boxes', 'arti_carousel_add_custom_meta_box' );
function arti_carousel_add_custom_meta_box (){
    add_meta_box(
        'arti_carousel_editor',
        __('Carousel', 'arti-carousel'),
        'arti_carousel_editor',
        'arti_carousel',
        'normal',
        'low'
    );
}
  • Shortcodes don't have IDs. Do you mean the post ID? – Jacob Peattie Sep 19 '19 at 9:37
  • Yes, sorry i mean the post-ID. But when i want to get the post ID with get_the_ID(), i get another ID, not the same like in the shortcode... – Artisolution Sep 19 '19 at 9:45
  • get_the_ID() gets the current post ID. Shortcodes don't have IDs, so if you used the same shortcode twice on the same page/post, they'd have the same ID. But if you want the current post ID outside the loop/shortcode, you'll need to use get_queried_object_id(). – Jacob Peattie Sep 19 '19 at 9:48
  • Okay, I guessed that already that the ids normaly can not be different. But it's strange why the get_the_ID(); 13 is the value and the for the shortcode the ID is 293... I'll have a look at it. Many thanks for the tip! – Artisolution Sep 19 '19 at 9:53
  • get_queried_object_id() returns the same odd 13 and not the post id -> 293... add_shortcode('arti_carousel', 'arti_carousel_display'); function arti_carousel_display($atts, $content = null) { $post_id = $atts['id']; – Artisolution Sep 19 '19 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.