[Resolved] Using elements to implement php script

Home Forums Support [Resolved] Using elements to implement php script

Home Forums Support Using elements to implement php script

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2323667
    Karsten

    Hey friends,

    could you please tell me the correct procedere to implement this script via Elements. I have tried hooks but always website is blocked afterwards. Thank you

    <?php
    add_filter( ‘generate_smooth_scroll_elements’, function( $elements ) {
    $elements[] = ‘a[href*=”#”]’;

    return $elements;
    } );

    #2323683
    David
    Staff
    Customer Support

    Hi there,

    so the GP Hook elements are for inserting code / content inside the themes templates.
    And this means you cannot hook in certain codes including add_filter as those are also a Hook ( a filter hook ) that has be to called from your functions.php.

    You must use one of the methods provided here instead:

    https://docs.generatepress.com/article/adding-php/

    #2323684
    Karsten

    Hi David,

    thank you very much for this clarification.

    Best

    #2324374
    David
    Staff
    Customer Support
    #2334786
    Lars

    I will continue on this thread and ask if there is any obvious problems adding this code in a GP-hook? Because I can’t make it work. Only with code snippets I can, but there shouldn’t be a problem using a GP-hook with this one?

    
    <?php
    
    function add_content_after_addtocart() {
    	
    	// get the current post/product ID
    	$current_product_id = get_the_ID();
         
      // get the product based on the ID
      $product = wc_get_product( $current_product_id );
         
      // get the "Checkout Page" URL
      $checkout_url = WC()->cart->get_checkout_url();
         
      // run only on simple products
      if( $product->is_type( 'simple' ) ){
          echo '<div class="clear-sec">';
          echo '</div>';
          echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Köp nu</a>';
          //echo '<a href="'.$checkout_url.'" class="buy-now button">Buy Now</a>';
            }
        }
    
    add_content_after_addtocart();
    
    ?>
    

    In code snippets it looks like this:

    function add_content_after_addtocart() {
         
            // get the current post/product ID
            $current_product_id = get_the_ID();
         
            // get the product based on the ID
            $product = wc_get_product( $current_product_id );
         
            // get the "Checkout Page" URL
            $checkout_url = WC()->cart->get_checkout_url();
         
            // run only on simple products
            if( $product->is_type( 'simple' ) ){
                echo '<div class="clear-sec">';
                echo '</div>';
                echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Köp nu</a>';
                //echo '<a href="'.$checkout_url.'" class="buy-now button">Buy Now</a>';
            }
        }
        add_action( 'generate_before_right_sidebar_content', 'add_content_after_addtocart' );

    But in GP-hooks I remove the action and add it into a GP-hook instead.

    #2334799
    Lars

    I solved it! It was just a matter of removing the cache-plugin.

    #2334820
    David
    Staff
    Customer Support

    Hi there,

    i can’t see any reason why it wouldn’t run.
    As long as:

    1. you have it wrapped in <?php // code here ?> tags
    2. have checked execute PHP in the GP hook
    3. have set the Display Rules so it only fires on pages where that code will work eg. Products

    Then i can’t see any reason it would not work.

    BUT…. i personally would not writher the function in the Hook element. Keep that in your child theme functions.php or code snippets. And only make the Callback in the Hook.

    #2334821
    David
    Staff
    Customer Support

    Glad to hear you got it working

    #2336567
    Lars

    David

    I am on the hunt to find the best way to implement code on the website (and I find Child Themes a bit bothering, don’t you agree?). It seems WPCode with its conditional logic is a far stronger choice than your suggested Code Snippets. Since it is also free and offers full functionality that further makes it a better choice.

    Your thoughts?

    #2336771
    David
    Staff
    Customer Support

    I have never used WPCode so i couldn’t advise on that.
    Personally, i like child themes or in some cases writing a custom plugin.
    If i were to list top reasons why, then ( scratching head ) i would say:

    a. keep code in WP Admin to the bare minimum
    b. provides a space for saving templates, fonts and media
    c. can always access it via FTP
    d. i edit the code directly in my IDE ( current fav their is VS Code )
    e. easy to transfer to another site.

    But this is just my preference. As those code snippets and wpcode plugins provide a lot of convenience and that has a lot of benefits for busy site builders.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.