Create a Wordpress Shortcode with functions in functions.php - php

The shortcode I'm trying to create should return the 1 Hungarian word described below, this should change month to month. The code has been placed into functions.php and returns nothing when the shortcode is used in a page or post. Any help would be much appreciated. It does work when used as simple php and html by using echo honapnev($honap);.
$honap = date("n");
function honapnev_shortcode( $ho ) {
if ($ho==1) {
return "Januári";
}
elseif ($ho==2) {
return "Februári";
}
elseif ($ho==3) {
return "Márciusi";
}
elseif ($ho==4) {
return "Áprilisi";
}
elseif ($ho==5) {
return "Májusi";
}
elseif ($ho==6) {
return "Júniusi";
}
elseif ($ho==7) {
return "Júliusi";
}
elseif ($ho==8) {
return "Augusztusi";
}
elseif ($ho==9) {
return "Szeptemberi";
}
elseif ($ho==10) {
return "Októberi";
}
elseif ($ho==11) {
return "Novemberi";
}
elseif ($ho==12) {
return "Decemberi";
}
}
add_shortcode( 'honapnev', 'honapnev_shortcode' );
When I remove the line add_shortcode( 'honapnev', 'honapnev_shortcode' );
the shortcode in brackets appears on the page...

function honapnev_shortcode($atts)
{
switch (date("n"))
{
case 1:
{
$month="Januári";
break;
}
case 2:
{
$month="Februári";
break;
}
case 3:
{
$month="Márciusi";
break;
}
case 4:
{
$month="Áprilisi";
break;
}
case 5:
{
$month="Májusi";
break;
}
case 6:
{
$month="Júniusi";
break;
}
case 7:
{
$month="Júliusi";
break;
}
case 8:
{
$month="Augusztusi";
break;
}
case 9:
{
$month="Szeptemberi";
break;
}
case 10:
{
$month="Októberi";
break;
}
case 11:
{
$month="Novemberi";
break;
}
case 12:
{
$month="Decemberi";
break;
}
default:
{
$month="Januári";
break;
}
}
return $month;
}
add_shortcode('honapnev', 'honapnev_shortcode');
or
function honapnev_shortcode($atts)
{
$months=array("Januári", "Februári", "Márciusi", "Áprilisi", "Májusi","Júniusi", "Júliusi", "Augusztusi","Szeptemberi", "Októberi", "Novemberi", "Decemberi");
return $months[date("n")-1];
}
add_shortcode('honapnev', 'honapnev_shortcode');

You need to parse the shortcode attributes in your function first, also, the elseif or switch statements can be replaced to get a short function.
function honapnev_shortcode($atts)
{
/**
* Get the shortcode attributes default value = 1
*/
$atts = shortcode_atts(array(
'ho' => 1,
), $atts, 'honapnev');
$ho = intval($atts['ho']);
/**
* Is better use an Array in this case
*/
$words = ['Januári', 'Februári', 'Márciusi', 'Áprilisi', 'Májusi', 'Júniusi', 'Júliusi', 'Augusztusi', 'Szeptemberi', 'Októberi', 'Novemberi', 'Decemberi'];
return $words[$ho - 1];
}
add_shortcode('honapnev', 'honapnev_shortcode');
The shortcode
[honapnev]
[honapnev ho="1"]
[honapnev ho="6"]
[honapnev ho="12"]

Related

Codeigniter, using if statement .. how to make it more simple?

Codeigniter, using if statement .. how to make it more simple?
if($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="x")
{
Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="y")
{
Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="z")
{
Do action
}
else
{
done
}
You could use a switch
if($this->session->userdata('logged_in')!="" ) {
switch ($this->session->userdata('stts'){
case "x":
Do action for x
break;
case "y":
Do action for y
break;
case "z":
Do action for z
break;
default:
Do action for othes
break;
}
} else {
// done
}
You can use switch case like this :
if($this->session->userdata('logged_in')!=""){
switch($this->session->userdata('stts')){
case "x":
// Do action
break;
case "y":
// Do action
break;
case "z":
// Do action
break;
default:
// Do action
break;
}
}
else{
//if session not set
}
Hope this helps!
if ($this->session->userdata('logged_in') != "" && in_array($this->session->userdata('stts'), ['x', 'y', 'z']))
{
//Do action
}
else
{
//done
}
using switch case you more beautify it.
if($this->session->userdata('logged_in')!="")
{
switch($this->session->userdata('stts'))
{
case "x":
//do some thing
break;
case "y":
//do some thing
break;
case "z":
//do some thing
break;
default:
//do some thing
}
} else {
// else condition
}
You can use a simple if/else and switch statement:
if ($this->session->userdata('logged_in') != "") {
switch ($this->session->userdata('stts')) {
case 'x':
// do action
break;
case 'y':
// do action
break;
case 'z':
// do action
break;
default:
// if neither x,y,z are stts
break;
}
} else {
// done
}

Prestashop remove one checkout step

I'm new to prestashop and I'm having major trouble removing the address(I want to have only Summary=Shrnutí, Login/Guest Checkout=Přihlásit se, Delivery=Doručení and Payment=Platba here https://www.enakupak.cz/objednavka?step=1) step,. I am using prestashop 1.6.1.5
I know I have to modify order-carrier.tpl file and have followed several posts here and there but couldn't get it done right.
Does any of you have any actual idea on how to do this ?
I think that it will be change in this part of OrderController.php but dont know how to concretly change it
switch ((int)$this->step) {
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
case OrderController::STEP_DELIVERY:
if (Tools::isSubmit('processAddress')) {
$this->processAddress();
}
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
break;
case OrderController::STEP_PAYMENT:
// Check that the conditions (so active) were accepted by the customer
$cgv = Tools::getValue('cgv') || $this->context->cookie->check_cgv;
if ($is_advanced_payment_api === false && Configuration::get('PS_CONDITIONS')
&& (!Validate::isBool($cgv) || $cgv == false)) {
Tools::redirect('index.php?controller=order&step=2');
}
if ($is_advanced_payment_api === false) {
Context::getContext()->cookie->check_cgv = true;
}
// Check the delivery option is set
if ($this->context->cart->isVirtualCart() === false) {
if (!Tools::getValue('delivery_option') && !Tools::getValue('id_carrier') && !$this->context->cart->delivery_option && !$this->context->cart->id_carrier) {
Tools::redirect('index.php?controller=order&step=2');
} elseif (!Tools::getValue('id_carrier') && !$this->context->cart->id_carrier) {
$deliveries_options = Tools::getValue('delivery_option');
if (!$deliveries_options) {
$deliveries_options = $this->context->cart->delivery_option;
}
foreach ($deliveries_options as $delivery_option) {
if (empty($delivery_option)) {
Tools::redirect('index.php?controller=order&step=2');
}
}
}
}
$this->autoStep();
// Bypass payment step if total is 0
if (($id_order = $this->_checkFreeOrder()) && $id_order) {
if ($this->context->customer->is_guest) {
$order = new Order((int)$id_order);
$email = $this->context->customer->email;
$this->context->customer->mylogout(); // If guest we clear the cookie for security reason
Tools::redirect('index.php?controller=guest-tracking&id_order='.urlencode($order->reference).'&email='.urlencode($email));
} else {
Tools::redirect('index.php?controller=history');
}
}
$this->_assignPayment();
if ($is_advanced_payment_api === true) {
$this->_assignAddress();
}
// assign some informations to display cart
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'order-payment.tpl');
break;
default:
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
}
What if you cann this code after first case - break:
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
After this case add this case:
case OrderController::STEP_ADDRESSES:
$this->_assignAddress();
$this->processAddressFormat();
if (Tools::getValue('multi-shipping') == 1) {
$this->_assignSummaryInformations();
$this->context->smarty->assign('product_list', $this->context->cart->getProducts());
$this->setTemplate(_PS_THEME_DIR_.'order-address-multishipping.tpl');
} else {
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
}
break;
Check, is it work.

Roll to another case in PHP switch

For example I have this:
switch ($action)
{
case 'my_action':
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
Is the example above going to go through the switch again and then call the first case my_action if the second case second_action has $this_is_true variable set to true?
If this doesn't work, what would be an alternative?
You can try something like this:
switch ($action)
{
case 'my_action':
case 'second_action':
if ($this_is_true || $action=='my_action')
{
doSomething();
}
else
{
doSomethingElse();
}
break;
}
When $action is equal to 'my_action' it will run through the case, as it finds no break sentence then it will run through the second case until it finds the break sentence.
Please give a look to example #3 in http://php.net/manual/en/control-structures.switch.php to find out more about no breaking switch cases.
No it won't. Just call doSomething(); in the 'second_action' case.
I wouldn't use a switch in this simple case, but if it is very long then maybe:
$do = false;
switch ($action)
{
case 'my_action':
$do = true;
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
$do = true;
}
else
{
doSomethingElse();
}
break;
}
if($do) { doSomething(); }
Try to do it this way:
do {
switch ($action)
{
case 'my_action':
$this_is_true = false;
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
} while($this_is_true);
Do not forget to switch $this_is_true to false where it needed!
But this code is not beautiful... May be you better to refactor your code.

Continue case in switch

I have following code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
break;
}
case (something3):
{
break;
}
}
Also the switch statement must check what one of cases give a TRUE, that is not the problem, the problem is, that i have now a case, where inside of case ... break; after checking other data, i wish to choose other switch-case, the one, that following him.
I have try do this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check)
{
something3 = true;
continue;
}
break;
}
case (something3):
{
break;
}
}
But PHP do not wish to go in to the case (something3): its break the full switch statement. How i can pass the rest of code of one case and jump to the next?
This is what's called a "fall-through". Try organizing your code with this concept.
switch (foo) {
// fall case 1 through 2
case 1:
case 2:
// something runs for case 1 and 2
break;
case 3:
// something runs for case 3
break;
}
Using your code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check) {
something3 = true;
}
else {
break;
}
}
case (something3):
{
break;
}
}
This will get to case something2 and run your check. If your check passes then it doesn't run the break statement which allows the switch to "fall through" and also do something3.
case (something2):
{
if(!check)
{
break;
}
}
Try this:
switch(true) {
case (something):
{
break;
{
case (something2):
{
if (!check) {
break;
}
}
case (something3):
{
break;
}
}
I had some similar situation and I came up with a solution which in your case would be like this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(!check)
{
break;
}
}
case (something3):
{
break;
}
}
You see that instead of checking the conditions to go to the next case, we checked if we should ignore the current one.

Show attachment pane by default

I am using latest version of TCPDF inorder to generate my PDF files and it is working fine
the problem where I stuck is I want to display file attachment annotation pane by default as it opens when user clicks on attachment image ...
I have tried the following :
$pdf->SetDisplayMode($zoom, $layout, $mode='UseAttachments');
but its not working.
Tell me how it is possible please ....
In my version of TCPDF, SetDisplayMode function looks like this:
public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
$this->ZoomMode = $zoom;
} else {
$this->Error('Incorrect zoom display mode: '.$zoom);
}
switch ($layout) {
//layout is set here
}
//page mode
switch ($mode) {
case 'UseNone': {
$this->PageMode = 'UseNone';
break;
}
case 'UseOutlines': {
$this->PageMode = 'UseOutlines';
break;
}
case 'UseThumbs': {
$this->PageMode = 'UseThumbs';
break;
}
case 'FullScreen': {
$this->PageMode = 'FullScreen';
break;
}
case 'UseOC': {
$this->PageMode = 'UseOC';
break;
}
case '': {
$this->PageMode = 'UseAttachments';
break;
}
default: {
$this->PageMode = 'UseNone';
}
}
}
Take a look at the last case. It will set a PageMode to "UseAttachments", but it looks to be defined in wrong way. Instead of case 'UseAttachments': there is case '':.
So, try to change your code to this:
$pdf->SetDisplayMode($zoom, $layout, '');

Categories