Im trying to add a button to the opposite side of entry-title specific page of wordpress customizr theme.
Here's my ref. https://presscustomizr.com/customizr/hooks-api/ theme page but I really dont know how to do.
I tried a php functions but it doesnt work.
Need some help please.
add_action ('__before_archive_title', 'button_beside_title');
function button_beside_title() {
if ( !is_page( array( '3154' ) ) ){
echo 'Text Example';
}}
Related
I added an additional field to my comment section of my wordpress page with the help of a little plugin, based on a tutorial https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/.
It's only one field I need (ZIP Code).
The plugin works fine, but it adds the custom field on every page. I just want it on one specific page.
I tried to wrap all the plugin code into one function that loads on the specific page:
add_action('template_redirect', 'load-on-certain-page');
function load-on-certain-page(){
if ( is_page(23) ) {
//Complete Plugin Code
}
}
Basically that works, but the check for the empty field isn't working anymore.
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if (empty( $_POST['title'] ) )
wp_die( __( 'Fehler: Bitte geben Sie Ihre Postleitzahl ein.' ) );
return $commentdata;
}
If I exclude the field check from my load-on-certain-page() function it checks the field everywhere so thats no solution. I also tried to add an additional condition to the if-statement (is_page()), but that does not work, too.
Can you point me into the right direction how to make my plugin functions work only on that certain page? And what "best practise" is to do it?
Thanks!
You can can possibly add an if/else statement to only show the field on that specific page...
if ( $post->ID === 23 ) {
// Input for zip code on page 23
} else {
// No input for zip code
}
Finally I got it.
The problem is, that is_page() etc. didn't work for the "preprocess_comment" filter.
But you can check the page ID the comment will be assigned to by using:
$post_id = $commentdata['comment_post_ID'];
I have made a custom post template named single-download.php after replicating single.php. However there are certain customizations which I dont want to show on single-download.php but am not able get a specific conditional to achieve the same.
I have tried if(is_page_template( 'single-download.php' )) and also if(!is_page_template( 'single.php' )) but both don't work.
Is there some way in which we can achieve POST template conditioning? Thanks
Okay I finally figured it out. We need to use
if( !is_singular( 'download' )
OR
if ( is_singular( 'download' ) ) {
// conditional content/code
}
It was that simple :)
I'm trying to figure out how to load the cart.min.js scripts not only if IS_CART but also if SHORTCODE_EXISTS('woocommrece_cart'), I can see that all the fronted scripts enqueues are in class-wc-frontend-scripts.php file and I don't want to override it, is there any hook that I can use to add another IF statement to the same file?
THANKS :)
after some testing
I want to explain my problem again:
I have a 'woocommerce_cart' shortcode that doesn't loads the scripts that it needs to work via ajax (like when updating cart and etc...) on other pages than the cart page
I see that in the class-wc-frontend-scripts.php the enqueue of that script goes only when is_cart() statement.
I am figuring out that I need to add if(shortcode_exists('woccommerce_cart')) so it would enqueue also when it is a shortcode.
I am adding that IF and I see that everything works on front end.
I am thinking that it is not a good idea to put that in the original plugin file because it would be overwritten with the next update.
I am trying to enqueue the script in my functions.php file with the new IF.
Nothing works....
Going back to the original file
On line 247 (here: https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-frontend-scripts.php) there is a function that adds more functionality to the ajax that without it it wont work...
Now I am wondering is there a way to add another IF to the same file using a hook? Or should I override all the file? (as I said before I think it is not a good practice because all the functions there wont get any updates).
So basically I know what I need to do, I just don't know what is the best way to do it and if it possible doing it with a hook or other function that maybe somebody knows and I'm not familiar with....
Thank you again for your help! :)
SHORT ANSWER
You need to define the WOOCOMMERCE_CART constant and make sure you're using a modern version of Woocommerce. I've tested this solution in Woocomerce 3.0.7.
if (!defined('WOOCOMMERCE_CART')) define( 'WOOCOMMERCE_CART', TRUE );
LONG ANSWER
I was having the same problem:
- I was calling the [woocommerce_cart] shortcode with the do_shortcode() function to display the cart in all the pages of my site.
- The cart was displaying well but the scripts (specifically cart.min.js) weren't loading in the pages. The effect of this is that the AJAX interactions weren't working.
The pages in my site were already running WC_Frontend_Scripts::init(). This function calls WC_Frontend_Scripts::load_scripts and that function loads our cart.min.js only if is_cart() (file wc-conditional-functions.php) is TRUE, as you already pointed.
Now, the current version of is_cart() evaluates TRUE when any of the following conditions is TRUE:
is_page( wc_get_page_id( 'cart' ) )
defined( 'WOOCOMMERCE_CART' )
wc_post_content_has_shortcode( 'woocommerce_cart' )
So, for me, the easiest way to fix the problem was to define the WOOCOMMERCE_CART constant.
Hope this helps. :-)
I had the exact same problem, combining cart and checkout pages broke the ajax auto cart
updates. I add the cart to the checkout page using:
function cart_on_checkout_page()
{
if ( is_wc_endpoint_url( 'order-received' ) ) return;
echo do_shortcode('[woocommerce_cart]');
}
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page', 5 );
My solution was to use the following code in functions.php to check if we are on the checkout page, and if so, enqueue/load the 'wc-cart' script. It's working with Woocommerce version 4.5.2.
function adjust_woocommerce_scripts()
{
if ( is_checkout() ) wp_enqueue_script( 'wc-cart' );
}
add_action( 'wp_enqueue_scripts', 'adjust_woocommerce_scripts', 11 );
You can adjust this function to perform whatever checks you need, such as if SHORTCODE_EXISTS('woocommrece_cart') - this means that the 'wc-cart' script is only loaded on the pages you need.
Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.
I have been using WPAlchemy for my custom meta boxes. I can usually get the meat box values to display using something like <?php $custom_mb->the_value('summary'); ?> but I am having trouble displaying the data in a genesis child theme. If use the above example I can get the post to display but it is at the very top of the page, even above the header. So I attempted to hook into the genesis_post_content hook using this
add_action('genesis_post_content', 'meta_content');
function meta_content() {
echo "Hello World";
}
I can echo Hello World this way but receive errors when trying the first example in the function. Any help would be greatly appreciated.
I do not know Genesis and I do not use WPAlchemy ( I avoid "frameworks" like fire ) but my logic dictates that if you can see the value at the top of the page like you described ( above header ) than the method the_value() is doing a direct echo, where you would need a return value.
The internal wordpress core logic dictates that whenever you have an echo function ( e.g. the_title() ) you would potentially have an equivalent return function ( e.g. get_the_title() ) and it would get the same function name with an added get_ prefix.
If the same wordpress logic is applied to those "frameworks" , or in this case to the WPAlchemy class , so instead of
$custom_mb->the_value('summary'); // if this is direct echo
you should be able to do :
$custom_mb->get_the_value('summary'); // then this should be return
Note that I did not tested it ( not using those "frameworks" , remember ?? ) but if it is indeed the case with WPAlchemy , than you would not need to invoke the genesis filter ( which by itself seems a bit wrong becasue the_content filter should be about the_content and not about meta_data but not knowing genesis I can not really say )