• Maybe dumb questions but is there a way to turn on/off one of my snippets from a page?
    Are there any shortcodes that come with Code Snippets?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    What do you mean by ‘turn on/off’? As in, prevent a snippet from running on certain pages? If so, there is! If you post an example snippet here, I can show you how to add the extra code to make this work.

    There is a built-in [code_snippet id=42] shortcode for displaying the code from a snippet inside a page or post. If you want to display actual content, I’d recommend taking a look at the example shortcode snippet and building something from that.

    Thread Starter r1bryan

    (@r1bryan)

    The plugin is called “code snippets” and when I click on it in the plugin admin page there appears checkboxes for running a particular set of code snippets. The code snippet I would like to mark as “on” or “off” from a different location than the admin page is shown below:

    /* Automatically set the image Title, Alt-Text, Caption & Description upon upload
    ———————————————————————–*/

    add_action( ‘add_attachment’, ‘my_set_image_meta_upon_image_upload’ );

    function my_set_image_meta_upon_image_upload( $post_ID ) {
    // Check if uploaded file is an image, else do nothing
    if ( wp_attachment_is_image( $post_ID ) ) {
    //$my_image_title = get_post( $post_ID )->post_title;
    //$filename = basename( get_attached_file( $doc_id ) );
    $my_image_title = basename( get_attached_file($post_ID ) );
    // Sanitize the title: remove hyphens, underscores & extra
    // spaces:
    $my_image_title = preg_replace( ‘%\s*[-_\s]+\s*%’, ‘ ‘,
    $my_image_title );
    $my_image_title = preg_replace( ‘/\.jpg$/’, ”,
    $my_image_title );
    // Sanitize the title: capitalize first letter of every word
    // (other letters lower case):
    $my_image_title = ucwords( strtolower( $my_image_title ) );
    //
    //remove begining – category
    $x = strpos($my_image_title,”By “);
    $x = $x + 3;
    $my_image_title = substr($my_image_title,$x);
    //remove date at end
    $my_image_title = substr($my_image_title,0,-9);
    //save title
    $x = strpos($my_image_title,” For “);
    $x = $x + 5;
    $title = substr($my_image_title,$x);
    //save name
    $x = strpos($my_image_title,” For “);
    $name = substr($my_image_title,0,$x);
    // title + name
    $my_image_title = $title . ” – ” . $name;
    //
    // Create an array with the image meta (Title, Caption,
    // Description) to be updated
    // Note: comment out the Excerpt/Caption or Content/Description
    // lines if not needed
    $my_image_meta = array(
    // Specify the image (ID) to be updated
    ‘ID’ => $post_ID,
    // Set image Title to sanitized title
    ‘post_title’ => $my_image_title,
    //remove classification text from title
    // Set image Caption (Excerpt) to sanitized title
    ‘post_excerpt’ => $my_image_title,
    // Set image Description (Content) to sanitized title
    ‘post_content’ => $my_image_title,
    );

    // Set the image Alt-Text
    update_post_meta( $post_ID, ‘_wp_attachment_image_alt’,
    $my_image_title );
    // Set the image meta (e.g. Title, Excerpt, Content)
    wp_update_post( $my_image_meta );
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to turn on/off a snippet from a page’ is closed to new replies.