Only view uername - wordpress

I want to view this code only with USERNAME, however the public display name taking over. how to only display USERNAME?
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->user_login;
return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

You are returning $user->display_name;. You need to return $display_name. Try the below code.
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->user_login;
return $display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

Shorter code:
function display_current_user_display_name () {
$user = wp_get_current_user();
return $user->user_login;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

Related

wordpress subscriber with contact form 7

Does anyone know if with contact form 7 it is possible to have a checkbox that when it is activated by the visitor, this visitor is registered as a wordpress user, as a subscriber?
After the comment of Howard E, the question changes to...
How can be used wpcf7_before_send_mail for register a new user with the data of the form?
something like this, but I don't know if it is completely correct...
add_action( 'wpcf7_before_send_mail', 'register_user' );
function register_user($cf7) {
$form_id = $cf7->id();
if ($form_id == 300 || $form_id == 301 || $form_id == 302) {
$submission = WPCF7_Submission :: get_instance();
}
if ($submission) {
if (empty($posted_data)) { return; }
$accept = $posted_data['acceptance-register-yes']; //acceptance check to be registered
if (empty($accept)) { return; }
$email = $posted_data['your-email'];
$name1 = $posted_data['your-name'];
$name2 = $posted_data['your-last-name'];
$name = ''; //here will go function to delete spaces and generate user name from $name1 + $name2
$pass = ''; //here will go function to random password
function create_user($n,$p,$e){
if (!username_exists($n) && !email_exists($e)) {
$user_id = wp_create_user($n, $p, $e);
$user = new WP_User($user_id);
$user->set_role( 'subscriber' );
}
}
create_user($name,$pass,$email);
}
}
many thanks
You can create a user and hook into wpcf7_before_send_mail like this.
function wp_create_custom_account(){
// Get the WPCF7 Submission instance
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
// Get the posted variables
$username = isset($posted_data['username'])?$posted_data['username']:'';
$password = isset($posted_data['password'])?$posted_data['password']:'';
$eml = isset($posted_data['eml'])?$posted_data['eml']:'';
// This can be radio or checkbox. Adjust your code accordingly
$radio = isset($posted_data['radio'][0])?$posted_data['radio'][0]:'';
if ($radio) {
$user = $username;
$pass = $password;
$email = eml;
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
}
}
}
}
add_action('wpcf7_before_send_mail','wp_create_custom_account');

show a message when updating a custom widget in wordpress backend

I'm developing a custom wordpress widget. The widget needs some data from the user and needs to check this data server-side.
I wrote the code that checks the data inside the function 'update' of the widget. When I press the button save of the widget the function update got called correctly and my validation is executed.
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName]) strip_tags($new_instance[$fieldName]) : '' );
}
$check = validate($new_instance);
return $instance;
}
What I need is to display a message to the user based on the result of the validation. How can I do this? For what I've seen the function update is called through ajax so I can't use an admin notice.
Is it possible?How can I do that?
Try below code
add_action('admin_notices', 'misha_custom_order_status_notices');
function misha_custom_order_status_notices() {
global $pagenow, $typenow;
if( get_transient( 'fx-admin-notice-panel' )){
echo "<div class=\"updated\"><p>Custom notification comes here</p></div>";
}
}
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName])?
strip_tags($new_instance[$fieldName]) :
''
);
}
$check = validate($new_instance);
set_transient( 'fx-admin-notice-panel', true, 5 );
return $instance;
}

Moving replicated codes somewhere appropriate and access them from all controllers

I want to be able to access a code from all my controllers so what is the best way of doing it? The code below is to handle form errors and I've been replicating it in every single controller in my project.
So I want to keep getErrorMessages() somewhere else and access it in controller below.
Note: I read about services but got confused and this example!
Example Controller:
class HelloController extends Controller
{
public function processAction(Request $request)
{
//More code here
if ($form->isValid() !== true)
{
$errors = $this->getErrorMessages($form);
return $this->render('SayHelloBundle:hello.html.twig',
array(
'page' => 'Say Hello',
'form' => $form->createView(),
'form_errors' => $errors
));
}
//More code here
}
private function getErrorMessages(FormInterface $form)
{
$errors = array();
foreach ($form->getErrors() as $error)
{
$errors[] = $error->getMessage();
}
foreach ($form->all() as $child)
{
if (! $child->isValid())
{
$options = $child->getConfig()->getOptions();
$field = $options['label'] ? $options['label'] : $child->getName();
$errors[$field] = implode('; ', $this->getErrorMessages($child));
}
}
return $errors;
}
}
You could create a class that has all the base operations in it and call that class (or set of classes) in your controllers.
OK did it :)
/var/www/html/local/sport/app/config/config.yml
imports:
- { resource: services.yml }
/var/www/html/local/sport/app/config/services.yml
parameters:
form_errors.class: Football\TeamBundle\Services\FormErrors
services:
form_errors:
class: %form_errors.class%
/var/www/html/local/sport/src/Football/TeamBundle/Services/FormErrors.php
namespace Football\TeamBundle\Services;
use Symfony\Component\Form\FormInterface;
class FormErrors
{
public function getErrors(FormInterface $form)
{
$errors = array();
//This part get global form errors (like csrf token error)
foreach ($form->getErrors() as $error)
{
$errors[] = $error->getMessage();
}
//This part get errors for form fields
foreach ($form->all() as $child)
{
if (! $child->isValid())
{
$options = $child->getConfig()->getOptions();
$field = $options['label'] ? $options['label'] : ucwords($child->getName());
//There can be more than one field error, that's why implode is here
$errors[strtolower($field)] = implode('; ', $this->getErrors($child));
}
}
return $errors;
}
}
controller
if ($form->isValid() !== true) {
$errors = $this->get('form_errors')->getErrors($form);
echo '<pre>'; print_r($errors); exit;
}

Create my own entity generator on symfony using doctrine generator

I am building an entity generator in order to update my DB's structure and data according to a distant one (DB), via SOAP WS...
I get the structure from an array, and am currently writing my entity classes by myself, with a file_put_content().
But as I advance, I can't help noticing that the Doctrine\ORM\Tools\EntityGenerator already does what I intend to do, way better I would ever be able to!!
So i was wondering if there was a way to use this implemented generator from my controller?
I mean, this is what Symfony is all about right? Using existing classes & bundles from anywhere in my code? :)
Any idea someOne?
Thx :)
You can use this code:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
class MyController extends Controller
{
private $generator;
public function generateEntityAction()
{
$format = "annotation"; //it can also be yml/php/xml
$fields = "title:string(255) body:text";
$withRepository = false; //true/false
$entity = Validators::validateEntityName("MyBundle:EntityName");
list($bundle, $entity) = $this->parseShortcutNotation($entity);
$format = Validators::validateFormat($format);
$fields = $this->parseFields($fields);
$bundle = $this->get('service_container')->get('kernel')->getBundle($bundle);
$generator = $this->getGenerator();
$generator->generate($bundle, $entity, $format, array_values($fields), $withRepository);
}
protected function createGenerator()
{
return new DoctrineEntityGenerator($this->get('service_container')->get('filesystem'), $this->get('service_container')->get('doctrine'));
}
protected function getSkeletonDirs(BundleInterface $bundle = null)
{
$skeletonDirs = array();
if (isset($bundle) && is_dir($dir = $bundle->getPath() . '/Resources/SensioGeneratorBundle/skeleton')) {
$skeletonDirs[] = $dir;
}
if (is_dir($dir = $this->get('service_container')->get('kernel')->getRootdir() . '/Resources/SensioGeneratorBundle/skeleton')) {
$skeletonDirs[] = $dir;
}
$skeletonDirs[] = __DIR__ . '/../Resources/skeleton';
$skeletonDirs[] = __DIR__ . '/../Resources';
return $skeletonDirs;
}
protected function getGenerator(BundleInterface $bundle = null)
{
if (null === $this->generator) {
$this->generator = $this->createGenerator();
$this->generator->setSkeletonDirs($this->getSkeletonDirs($bundle));
}
return $this->generator;
}
protected function parseShortcutNotation($shortcut)
{
$entity = str_replace('/', '\\', $shortcut);
if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Blog/Post)', $entity));
}
return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
}
private function parseFields($input)
{
if (is_array($input)) {
return $input;
}
$fields = array();
foreach (explode(' ', $input) as $value) {
$elements = explode(':', $value);
$name = $elements[0];
if (strlen($name)) {
$type = isset($elements[1]) ? $elements[1] : 'string';
preg_match_all('/(.*)\((.*)\)/', $type, $matches);
$type = isset($matches[1][0]) ? $matches[1][0] : $type;
$length = isset($matches[2][0]) ? $matches[2][0] : null;
$fields[$name] = array('fieldName' => $name, 'type' => $type, 'length' => $length);
}
}
return $fields;
}
}
Mostly, it comes from Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineEntityCommand and Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand

Hook into the user_change process

I have a database with user information from a another project. Now I want to link this user data with my new wordpress installation. I want, if a user is about to login and has no Wordpress profile yet, but a profile is in that other database already, then wordpress needs to create a profile based on the data, that already exists in the other database.
I'm already done with the login and it works fine...
add_filter('authenticate', 'check_login', 40, 3);
function check_login($user, $username, $password) {
$user = get_user_by( 'login', $username );
$hash = kbHash($password);
if ( $user ) {
if (!wp_check_password( $hash, $user->data->user_pass, $user->ID) ) {
$user = NULL;
}
} elseif ($kbCredentials = isKanuboxUser($username)) {
if ($kbCredentials['hash'] == $hash)) {
$user_id = wp_create_user( $kbCredentials['username'], $kbCredentials['hash'], $kbCredentials['mail'] );
$user = get_userdata( $user_id );
} else {
$user = NULL;
}
} else {
$user = NULL;
}
return $user;
}
function kbHash($password) {
//TODO: additional hashing from other project
$hash = $password;
return $hash;
}
Now, the problem is, if some user edits his password, I need to do this hashing too and update it in the original database. Are there appropriate hooks inside wordpress?
I only found this: add_action( 'profile_update', 'some_function' );
But in that function I am not able to add my hashing to the plain text password before the wp-hashing is applied.
How to solve this?

Resources