PHP - Syntax Error - php

I have a syntax error in my php code and just cannot figure out how should I fix it. I've been trying for ages and narrowed down the faulty code to the following :
<?php
global $post;
$tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ) );
if ( ! class_exists( 'Tax_Meta_Boxes' ) ) :
class Tax_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'tax_meta_data' ) );
}
}
?>
I ran the code through the PHP checker, the error seems to be with the last line and ?>.
I tried fiddling around with it, remove it, but error still there.
The error is caused by the function :
if ( ! class_exists( 'Tax_Meta_Boxes' ) ) :
class Tax_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'tax_meta_data' ) );
}
}
If I remove the function, my error is gone, but what is wrong in the code of this function, how can I fix it ?

You have missed endif; for your if()
<?php
global $post;
$tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ) );
if ( ! class_exists( 'Tax_Meta_Boxes' ) ) :
class Tax_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'tax_meta_data' ) );
}
}
endif;// this line
?>

Related

How can I add a route to the index in my WordPress plugin using generate_rewrite_rules?

I'm trying to add the route /.well-known/webfinger to WordPress in a plugin, e.g. http://exampleblog.com/.well-known/webfinger. I'm using the generate_rewrite_rules, parse_request, and query_vars hooks to load up some code that should run when the URL is matched. Here's what I've got:
// includes/server/api.php
namespace api;
function generate_rewrite_rules( $wp_rewrite ) {
$dot_well_known = array(
'.well-known/webfinger' => 'index.php?well-known=webfinger'
);
$wp_rewrite->rules = $dot_well_known + $wp_rewrite->rules;
}
function check_flush_rules() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['.well-known/webfinger'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
function parse_request( $req ) {
if ( ! array_key_exists( 'well-known', $req->query_vars ) ) {
return;
}
if ( $req->query_vars['well-known'] === 'webfinger' ) {
do_action( 'well_known_webfinger', $req->query_vars );
}
}
function query_vars( $query_vars ) {
$query_vars[] = 'well-known';
return $query_vars;
}
// includes/init.php
namespace init;
require_once plugin_dir_path( __FILE__ ) . 'server/api.php';
add_action( 'my_plugin_load', function() {
add_action( 'generate_rewrite_rules', '\api\generate_rewrite_rules' );
add_action( 'parse_request', '\api\parse_request' );
add_filter( 'query_vars', '\api\query_vars' );
\api\check_flush_rules();
} );
// my_plugin.php (plugin entrypoint)
require_once plugin_dir_path( __FILE__ ) . 'includes/init.php';
function my_plugin_load() {
do_action( 'my_plugin_load' );
}
add_action( 'plugins_loaded', 'my_plugin_load' );
However, when I run a local WordPress instance (via php -S localhost:8080) I'm getting a 404 Not Found status when I attempt to visit http://localhost:8080/.well-known/webfinger.
What am I doing wrong?
It turns out the problem was that I was running WordPress via php -S localhost:8080. When I ran a proper Apache webserver locally generate_rewrite_rules worked as it was supposed to.

One save function for all metaboxes

I have a function to set up metaboxes. This creates them when a class is instantiated with a custom name.
Now the init() execute two add_action functions. One to add the metabox and one to save the metabox.
Creating the metabox works fine. However saving doesn't
It only saves the last created metabox to the database.
class metaBox {
final public function init() {
add_action( 'add_meta_boxes', [ $this, 'add' ] );
add_action( 'save_post', [ $this, 'save' ] );
}
public $CMB_Name;// for custom name
public function setName( $CMB_Name ) {
$this->CMB_Name = $CMB_Name;
}
public function add() { // to add the metabox
add_meta_box(
$this->CMB_Name,
__( $this->CMB_Name, 'plugin' ),
[ $this, 'display' ],
'page',
'normal',
'high'
);
}
public function save( $post_id ) {// to save the metabox
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST['nonce_check_value'] ) && wp_verify_nonce( $_POST['nonce_check_value'], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
return;
}
if ( isset( $_POST[ $this->CMB_Name . '-text' ] ) ) {
update_post_meta( $post_id, $this->CMB_Name . '-text', sanitize_text_field( $_POST[ $this->CMB_Name . '-text' ] ) );
}
}
}
What I don't understand is that it doesn't save both values while there are unique nonces and names to the metaboxes .
The objects are created like this:
function mbe_start() {
$plugin = new testForm();
$plugin->setName( "OOP Plugin name" );
$plugin->init();
$plugin1 = new anotherForm();
$plugin1->setName( "whaat" );
$plugin1->init();
}
mbe_start();
Why doesn't the save function work? I mean it's principle is exactly like the one that adds the metabox.
If I disable the isset() function it throws me a notice about an Undefined index for the first metabox. Not really sure why and/or how to fill it.
OMG.....
Got it working. The reason is that the name given contains spaces and capital letters. This caused the problem.
Fixed it by replacing the name with
strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $this->CMB_Name)));

Wordpress: Include new template for a page using query var with rewrite endpoint

I am trying to load a new page template when a query var is appended at the end of my page url:
Original url: example.com/testpage/
with variable added to the end: example.com/testpage/amp
Then it would load up a custom php template.
This seems like a straight forward operation, but I cannot get it to work.
The url loads with the /amp variable at the end, but the template does not load. If I remove the condition "get_query_var('amp')" then it loads up the template no problem. What am I missing? Thanks :)
Here is my working code:
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if ( get_query_var( 'amp' ) && is_page() ) {
$new_template = locate_template( array( 'amptemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Found a good fix on my own. Here is the code if it would help anyone.
Adding 'amp' after a page or post will load up different templates for amp versions of the page.
example.com/samplepage/amp
or
example.com/samplepost/amp
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES | EP_PERMALINK );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if(isset( $wp_query->query['amp'] ) && is_page()){
$new_template = locate_template( array( 'amppagetemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
if(isset( $wp_query->query['amp'] ) && is_single()){
$new_template = locate_template( array( 'ampposttemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}

Edit the default rss url of wordpress

I need to change the default rss url of my website:
from example.com/feed to example.com/MyfeedName
Update:
what i tried so far is to create another Url feed but i need to remove firstexample.com/feed:
add_action( 'init', function()
{
add_feed( 'secretfeed', 'do_feed_rss2' );
});
add_action( 'pre_get_posts', function( \WP_Query $q )
{
if( $q->is_feed( 'secretfeed' ) )
add_filter( 'option_rss_use_excerpt', '__return_false' );
} );
do you have any idea how to just edit example.com/feed or how to delete it without losing rss functions ?
I found my answer here :
https://wordpress.stackexchange.com/a/214883/71314
function remove_feed( $feedname ) {
global $wp_rewrite;
if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds = array_diff( $wp_rewrite->feeds, array( $feedname ) );
}
$hook = 'do_feed_' . $feedname;
// Remove default function hook
remove_all_actions( $hook );
add_action( $hook, $hook );
return $hook;
}
Usage:
remove_feed( 'feed' );

Combining two functions for WooCommerce

Im trying to combine these two functions in my functions.php file. I have tried a couple variations but cant seem to fine the right one. It works as it is broken into two functions but now my curiosity has got the best of me.
Im removing fields in my checkout page in Woo Commerce, two fields. I cant figure out to to call them both in one function so I just used two to make it work but would just like to know how to make it into one.
function custom_override_checkout_fields_1( $fields ) {
if ( ! current_user_can( 'dealer_group' ) && isset( $fields['billing']['rep_name'] ) ) {
unset( $fields['billing']['rep_name'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields_1' );
function custom_override_checkout_fields_2( $fields ) {
if ( ! current_user_can( 'dealer_group' ) && isset( $fields['billing']['billing_company'] ) ) {
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields_2' );
You just refactor those two functions into one.
No need to check for isset as unset will not do anything if it isn't set.
function custom_override_fields($fields) {
if ( ! current_user_can( 'dealer_group' ) ) {
unset( $fields['billing']['rep_name'] );
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
function custom_override_fields($fields) {
if ( ! current_user_can( 'dealer_group' ) && isset( $fields['billing']['rep_name'] ) ) {
unset( $fields['billing']['rep_name'] );
}
if ( ! current_user_can( 'dealer_group' ) && isset( $fields['billing']['billing_company'] ) ) {
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
Just combined the two ifs.

Categories