- This topic has 9 replies, 3 voices, and was last updated 6 months, 1 week ago by
David.
-
AuthorPosts
-
August 25, 2022 at 7:57 am #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;
} );August 25, 2022 at 8:08 am #2323683David
StaffCustomer SupportHi there,
so the GP Hook elements are for inserting code / content inside the themes templates.
And this means you cannot hook in certain codes includingadd_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/
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 25, 2022 at 8:09 am #2323684Karsten
Hi David,
thank you very much for this clarification.
Best
August 26, 2022 at 3:22 am #2324374David
StaffCustomer SupportYou’re welcome
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 6, 2022 at 5:56 am #2334786Lars
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.
September 6, 2022 at 6:09 am #2334799Lars
I solved it! It was just a matter of removing the cache-plugin.
September 6, 2022 at 6:27 am #2334820David
StaffCustomer SupportHi 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. ProductsThen 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.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 6, 2022 at 6:28 am #2334821David
StaffCustomer SupportGlad to hear you got it working
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 7, 2022 at 10:56 pm #2336567Lars
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?
September 8, 2022 at 2:51 am #2336771David
StaffCustomer SupportI 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.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.