Extra fields for wordpress user management in dashboard - php

I need to add extra fields to the Add new user and Edit user pages in wordpress admin dashboard.
I can add this to Edit user using the hook edit_user_profile. But how to add it in Add new user page?
Also I want to add some extra columns to the user wp list table.
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
}

If you consult the core code, in /wp-admin/user-new.php you can find two hooks:
user_new_form_tag – before the existing form fields
user_new_form – after the existing form fields

If you check /wp-admin/user-new.php you'll see two do_action, though I never tried using any of these hooks
hook to add tags or attribute on form
<form method="post" name="adduser" id="adduser" class="validate" novalidate="novalidate"<?php
/**
* Fires inside the adduser form tag.
*
* #since 3.0.0
*/
do_action( 'user_new_form_tag' );
?>>
and
/**
* Fires at the end of the new user form.
*
* Passes a contextual string to make both types of new user forms
* uniquely targetable. Contexts are 'add-existing-user' (Multisite),
* and 'add-new-user' (single site and network admin).
*
* #since 3.7.0
*
* #param string $type A contextual string specifying which type of new user form the hook follows.
*/
do_action( 'user_new_form', 'add-existing-user' );
and
<?php
/** This action is documented in wp-admin/user-new.php */
do_action( 'user_new_form', 'add-new-user' );
?>
You might also want to check user_register hook

Related

Modify product the moment it's created by WooCommerce Rest API

I have an ERP system creating products in WooCommerce and I need them to be private instead of published.
I tried the hook woocommerce_rest_insert_product but it's not doing anything. I've tried adding it on a plugin and on a mu-plugin using plugins_loaded action.
I found the hook inside the class WC_REST_Products_V1_Controller, in theory it should work...
/**
* Fires after a single item is created or updated via the REST API.
*
* #param WP_Post $post Post data.
* #param WP_REST_Request $request Request object.
* #param boolean $creating True when creating item, false when updating.
*/
do_action( 'woocommerce_rest_insert_product', $post, $request, false );
I couldn't find the answer to "why" it's not working.
What I found is this WordPress.org forum post showing an alternative that works:
add_action( 'woocommerce_new_product', function($id, $product ){
// your thing
}, 10, 2);
I know this is a late answer but it could make sense for others getting here to read this.
I am pretty sure you are using the latest v3 of the API.
The problem is that the action you mentioned can be found in WC_REST_Products_V1_Controller which has the endpoint namespace set to:
protected $namespace = 'wc/v1';
This means it is not usable on the v3.
If you go through the Woocommerce REST controllers in Version 3 you will reach this file:
includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
which has the namespace set to wc/v3;
Here, the class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller.
In the WC_REST_Products_V2_Controller there are the create_item and update_item methods. Both of these methods look very similar to what was in v1 but, the action name you are looking for is changed to:
/**
* Fires after a single object is created or updated via the REST API.
*
* #param WC_Data $object Inserted object.
* #param WP_REST_Request $request Request object.
* #param boolean $creating True when creating object, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true );
So, your code should look something like:
add_action(
"woocommerce_rest_insert_product_object",
function($product, $request, $creating ){
// do something here
}, 10, 3
);
The big difference between woocommerce_new_product and woocommerce_rest_insert_{$this->post_type}_object is that woocommerce_new_product is triggered on all create actions, not only on the REST create.

TYPO3 Hook for preview for multiple languages

I have a TYPO3 installation with multiple languages.
Now I want to create a function that if i.e. a french editor, with french settings, goes to the View (or preview of a page) that he gets the french version and not the default language.
My first idea was to use the PageLayoutViewDrawItemHookInterface and then change the URL for the right language
in ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['tx_editordashboard'] =
\Vendor\Editordashboard\Hooks\Backend\CheckUserCountry::class;
in my CheckUserCountry.php class
<?php
namespace TRUMPF\Editordashboard\Hooks\Backend;
use \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use \TYPO3\CMS\Backend\View\PageLayoutView;
class CheckUserCountry implements PageLayoutViewDrawItemHookInterface
{
/**
* Preprocesses the preview rendering of a content element.
*
* #param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling
parent object
* #param bool $drawItem Whether to draw the item using the default
functionalities
* #param string $headerContent Header content
* #param string $itemContent Item content
* #param array $row Record row of tt_content
*/
public function preProcess(PageLayoutView &$parentObject, &$drawItem,
&$headerContent, &$itemContent, array &$row) {echo "bam";}
}
But the Hook doesn't work if I click on the "View" Button. Any ideas so solve the problem?

How do I unhook WordPress action hook in plugin file?

I am trying to unhook and modify an action from within my child themes functions.php file.
The WordPress plugin Sensei, adds this action in line 86 of this document.
https://github.com/Automattic/sensei/blob/master/includes/class-sensei-modules.php#L86
The action references a function further down the page that is responsible for outputting a dynamic header element.
/**
* Show the title modules on the single course template.
*
* Function is hooked into sensei_single_course_modules_before.
*
* #since 1.8.0
* #return void
*/
public function course_modules_title( ) {
if( sensei_module_has_lessons() ){
echo '<header><h2>' . __('Modules', 'woothemes-sensei') . '</h2></header>';
}
}
My goal here is to change the html currently output as 'Modules' to something else.
I have tried to the following in my child themes functions.php file but neither seem to be working.
remove_action( 'sensei_single_course_modules_before', array( 'Sensei_Core_Modules', 'course_modules_title' ), 20);
remove_action( 'sensei_single_course_modules_before', array( 'Sensei()->Sensei_Core_Modules', 'course_modules_title' ), 20);
The issue is, I do not know how to determine which initial parameter, to add to the array to call the correct class. Because I am accessing it externally I cannot use $this like it is being used in the core file.
In order to remove the action you have to find the instance of the class. This is an assumption since I don't have access to the source code of Sensei but big chance there is one since most WordPress plug-ins use this method.
When you find the instance name you can load it using global $senseiInstance - replace this with the actual name of the variable.
Then you can remove the action using for example this code:
remove_action( 'sensei_single_course_modules_before', array( $senseiInstance, 'course_modules_title' ), 20);
More information can be found in for example this article: https://www.sitepoint.com/digging-deeper-wordpress-hooks-filters.
Hope this helps you out!

Stop TinyMCE stripping empty tags in WordPress - yes I have researched already

Like many other people, I have a problem with TinyMCE stripping HTML tags - specifically the empty ones I'm using with Font Awesome.
I have researched and tried solutions and nothing has worked. I'm not especially strong with PHP, but the problem I'm running into is this: everybody says modifiy the tinyMCE.init function in the tinymce.js file. However being in the newest version of WP, I don't have that. What I have class-wp-editor.php, wherein lives this:
/*
* For people who really REALLY know what they're doing with TinyMCE
* You can modify $mceInit to add, remove, change elements of the config
* before tinyMCE.init. Setting "valid_elements", "invalid_elements"
* and "extended_valid_elements" can be done through this filter. Best
* is to use the default cleanup by not specifying valid_elements,
* as TinyMCE checks against the full set of HTML 5.0 elements and attributes.
*/
if ( $set['teeny'] ) {
/**
* Filter the teenyMCE config before init.
*
* #since 2.7.0
*
* #param array $mceInit An array with teenyMCE config.
* #param string $editor_id Unique editor identifier, e.g. 'content'.
*/
$mceInit = apply_filters( 'teeny_mce_before_init', $mceInit, $editor_id );
} else {
/**
* Filter the TinyMCE config before init.
*
* #since 2.5.0
*
* #param array $mceInit An array with TinyMCE config.
* #param string $editor_id Unique editor identifier, e.g. 'content'.
*/
$mceInit = apply_filters( 'tiny_mce_before_init', $mceInit, $editor_id );
}
Now I know I have to do something with valid_elements, or extended_valid_elements, or verify_html, but I don't know how to do it. I can see from the comments where to put it, but I don't know which to use or the proper syntax.
I found this fiddle: http://fiddle.tinymce.com/j9baab/1 but like I said I don't have that function anywhere in WP.
Please help!
You really don't want to modify the class-wp-editor.php file as each time you update WordPress it will get overwritten.
The easiest way to extend/modify the settings of TinyMCE is to build a simple WordPress Plugin and tie into the hooks WordPress provides to change the editor's settings.
To solve your particular desire to add options to the init you want to look at
the 'tiny_mce_before_init' hook. You might do something like this in the plugin:
add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {
// $opt is the existing array of options for TinyMCE
// We simply add a new array element where the name is the name
// of the TinyMCE configuration setting. The value of the array
// object is the value to be used in the TinyMCE config.
$opt['extended_valid_elements'] = '*[*]';
return $opt;
}
Writing a simple WP plugin is not too hard - there are plenty of examples on the web. For something this simple its really just a single PHP file. Once you have the plugin built you just install and activate it. Once activated, your code is run each time TinyMCE is invoked and your options are injected into the init code.
EDIT: Here is the code from the OPs comment (easier to read than how the comments are formatted):
<?php
/**
* Plugin Name: Disable TinyMCE Filters
* Plugin URI: http://mindspyder.com
* Description: This plugin disables annoying TinyMCE Filters.
* Version: 1.0.0
* Author: Brandon Snow
* Author URI: http://mindspyder.com
* License: GPL2
*/
add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {
// $opt is the existing array of options for TinyMCE
// We simply add a new array element where the name is the name
// of the TinyMCE configuration setting. The value of the array
// object is the value to be used in the TinyMCE config.
$opt['extended_valid_elements'] = '*[*]';
return $opt;
}
?>

TYPO3 extension createAction nothing happens

I am currently trying to write a simple input form extension: the user enters the input field values, the submit action inserts the values into the database and then redirects to an external payment service.
Unfortunately, the createAction function does not show any reaction after a click on the submit button.
For test purposes, I just want to output a text after the submit. But not even that works.
If I use the exact same function of flashMessageContainer in the newAction, it works: the message is displayed immediately. But when I want to show it after a click on the submit button, nothing but a page reload happens.
What could be the problem?
Resources / Private / Templates / Payment / New.html:
<f:form method="post" controller="Payment" action="create" id="newPayment" name="newPayment" object="{newPayment}">
<f:render partial="Payment/FormFields" />
<div class="buttons row">
<div class="col c6">
<f:form.submit value="{f:translate(key:'tx_chilipayment_domain_model_payment.submit')}" />
</div>
</div>
......
Classes / Controller / PaymentController.php:
<?php
namespace chilischarf\ChiliPayment\Controller;
class PaymentController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
* paymentRepository
*
* #var \chilischarf\ChiliPayment\Domain\Repository\PaymentRepository
* #inject
*/
protected $paymentRepository;
/**
* action new
*
* #param \chilischarf\ChiliPayment\Domain\Model\Payment $newPayment
* #dontvalidate $newPayment
* #return void
*/
public function newAction(\chilischarf\ChiliPayment\Domain\Model\Payment $newPayment = NULL) {
$this -> view -> assign('newPayment', $newPayment);
}
/**
* action create
*
* #param \chilischarf\ChiliPayment\Domain\Model\Payment $newPayment
* #return void
*/
public function createAction(\chilischarf\ChiliPayment\Domain\Model\Payment $newPayment) {
$this -> flashMessageContainer -> add('Your new Payment was created.');
}
}
?>
Usually you dont want your createAction to render anything. You just want it to validate and persist the user input and then redirect to another action, where e.g. a flash message is rendered. That being said, the problem you describe can have several causes, so I will point to a few issues you might have or your problem may be related to:
Do you have a Create.html Template in Resources/Private/Templates/Payment/Create.html? This is the template the create action will render.
Do you have a <f:flashMessages /> viewHelper in this template? Since you dont assgin anything to your view in your createAction (which is perfectly fine if you dont plan to render anything here, as mentioned above) this is he only "dynamically" created content.
Do you reach your createAction after submitting your form? Or is something with the validation going wrong (in that case you will be redirected back to your newAction with a default flashMessage that an error occured while trying to call createAction)? You can figure that out, when you add a die(); to your createAction. After submiting you'll see a white page if your creatAction was called successfully.
Is 'create' a valid action for your controller as configured in your ext_localconf.php? If not, add it.

Categories