add function to function php

Adding javascript to wordpress functions.php

JavaScript(s) Are Not Loading

I found this really awesome static html/css template online and wanted to convert it to a wordpress theme but after the conversion i noticed the html/css is loading from inspect element view, although (visually) only the menu, section backgrounds and footer are showing on the screen while the remaining body content seems to be hiding.

Check my functions.php for any errors enqueuing the javascript. The alert works but the other scripts seem to not load. I’m sure i made a rookie mistake and coded this incorrectly.

Im developing on xampp.

add function to function php

3 Answers 3

To load the JS files, you need to:

This will be the same with the missing images: the directory structure of the HTML template is not the same as of the WordPress installation. You just have to correct that, and everything will be fine.

add function to function php

Working with the theme in your wordpress theme updated june 20 2019 zip:

change: «/images/ to » /images/

change: url(/images/ to url( /images/

change: url(‘/images/ to url(‘ /images/

suggestion: use url(‘. ‘) instead of url(. ) (this way you’ll be sure that it’s treated like a string), but at least only use one syntax in one theme. It’s easier to manage, if you create a convention for your template, and stick to that.

not found: wp-content/themes/eLeadWorks/js/jquery.timepicker.min.js (the file is not in that library, so I commented register and enqueue out)

not found: wp-content/themes/eLeadWorks/js/example.js (the file is not in that library, so I commented register and enqueue out)

rename script handles (suggestion): don’t use version numbers in script registering handles. This way you’ll not be able to freely update versions if you need to. (so jquery-migrate-3.0.1.min is not OK, jquery-migrate is OK)

After these modifications

So, either add an element with id=»map» (where Google Maps can place the map), or modify your code to check for mapElement ‘s value (in google-map.js):

After all these modifications I don’t see a single error in my console.

Источник

PHP Functions

The real power of PHP comes from its functions.

PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.

PHP Built-in Functions

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

Please check out our PHP reference for a complete overview of the PHP built-in functions.

PHP User Defined Functions

Besides the built-in PHP functions, it is possible to create your own functions.

Create a User Defined Function in PHP

A user-defined function declaration starts with the word function :

Syntax

Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.

Tip: Give the function a name that reflects what the function does!

In the example below, we create a function named «writeMsg()». The opening curly brace ( < ) indicates the beginning of the function code, and the closing curly brace ( >) indicates the end of the function. The function outputs «Hello world!». To call the function, just write its name followed by brackets ():

Example

PHP Function Arguments

Information can be passed to functions through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

Example

Example

PHP is a Loosely Typed Language

In the example above, notice that we did not have to tell PHP which data type the variable is.

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a «Fatal Error» if the data type mismatches.

In the following example we try to send both a number and a string to the function without using strict :

Example

In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:

Example

The strict declaration forces things to be used in the intended way.

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

Example

To let a function return a value, use the return statement:

Example

PHP Return Type Declarations

PHP 7 also supports Type Declarations for the return statement. Like with the type declaration for function arguments, by enabling the strict requirement, it will throw a «Fatal Error» on a type mismatch.

To declare a type for the function return, add a colon ( : ) and the type right before the opening curly ( < )bracket when declaring the function.

In the following example we specify the return type for the function:

Example

You can specify a different return type, than the argument types, but make sure the return is the correct type:

Example

Passing Arguments by Reference

In PHP, arguments are usually passed by value, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed.

When a function argument is passed by reference, changes to the argument also change the variable that was passed in. To turn a function argument into a reference, the & operator is used:

Example

Use a pass-by-reference argument to update a variable:

Источник

Function inside a function.?

This code produces the result as 56.

Any idea what is going inside? I am confused.

8 Answers 8

X returns (value +3), while Y returns (value*2)

Although functions are not limited in scope (which means that you can safely ‘nest’ function definitions), this particular example is prone to errors:

The solution to both would be to split the code, so that both functions are declared independent of each other:

This is also a lot more readable.

Note that PHP doesn’t really support «nested functions», as in defined only in the scope of the parent function. All functions are defined globally. See the docs.

add function to function php

Not sure what the author of that code wanted to achieve. Definining a function inside another function does NOT mean that the inner function is only visible inside the outer function. After calling x() the first time, the y() function will be in global scope as well.

add function to function php

add function to function php

add function to function php

It is possible to define a function from inside another function. the inner function does not exist until the outer function gets executed.

Simple thing you can not call function y before executed x

Your query is doing 7 * 8

x(4) = 4+3 = 7 and y(4) = 4*2 = 8

what happens is when function x is called it creates function y, it does not run it.

function inside a function or so called nested functions are very usable if you need to do some recursion processes such as looping true multiple layer of array or a file tree without multiple loops or sometimes i use it to avoid creating classes for small jobs which require dividing and isolating functionality among multiple functions. but before you go for nested functions you have to understand that

so is this mean you cant use nested functions? No, you can with the below workarounds

first method is to block the child function being re declaring into global function stack by using conditional block with function exists, this will prevent the function being declared multiple times into global function stack.

and the second method will be limiting the function scope of child to local instead of global, to do that you have to define the function as a Anonymous function and assign it to a local variable, then the function will only be available in local scope and will re declared and invokes every time you call the main function.

remember the child will not be available outside the main function or global function stack

Источник

I am running a wordpress multisite with 2 blogs ( site1.com and site2.com )

The entire site is sharing the same theme as well as the functions.php file.

The filter is as follows :

What I have done instead is that I have created a new plugin, I have added the function to the plugin and activated the plugin only on site2.com.

3 Answers 3

In my opinion the best way to target one subsite within multisite is to use the get_current_blog_id() function within the function called by the filter. Here’s an example of some code that would work:

You can grab the ID of the site you want to target by going to https://example.org/wp-admin/network/sites.php where https://example.org is replaced with your domain.

There are a few benefits to this approach over others listed here.

This is a bit tricky. You might have to check for the site name or id before adding the filter.

Your function remains as is but the filter part becomes:

You can read more about get_current_site() here.

I know this was from a while ago but I’ve just been trying to add a function to only one multisite too. The method is a little confusing at first as you need to jump from the multisite admin to the main network admin. The way to do this is to create a child theme via the main network admin (is used a child theme creator plugin. There’s quite a few). Whilst still in the network admin, select the main ‘themes’ heading then ‘editor’ to show current theme then ‘select the theme to edit’ at the top right & select the child theme from the dropdown. Add the new code to the functions.php Once you’ve added what you need, switch back to the dashboard for the multisite you needed the function for and activate the child theme. If you need another function for a different multisite, go back to the network admin & create another child theme (I currently have twentyseventeen as my main theme then child1 for multisite 1 and child2 for multisite 2 It’s a little convoluted but once you understand that these changes must be done from the network admin, it’s pretty easy. I hope that helps.

Not the answer you’re looking for? Browse other questions tagged php wordpress or ask your own question.

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.16.40224

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Add function to function php

To experiment on performance of pass-by-reference and pass-by-value, I used this script. Conclusions are below.

3 valuue yes 129 s
4 reference yes 66 us

1. PHP is already smart about zero-copy / copy-on-write. A function call does NOT copy the data unless it needs to; the data is
only copied on write. That’s why #1 and #2 take similar times, whereas #3 takes 2 million times longer than #4.
[You never need to use &$array to ask the compiler to do a zero-copy optimisation; it can work that out for itself.]

2. You do use &$array to tell the compiler «it is OK for the function to over-write my argument in place, I don’t need the original
any more.» This can make a huge difference to performance when we have large amounts of memory to copy.
(This is the only way it is done in C, arrays are always passed as pointers)

3. The other use of & is as a way to specify where data should be *returned*. (e.g. as used by exec() ).
(This is a C-like way of passing pointers for outputs, whereas PHP functions normally return complex types, or multiple answers
in an array)

5. Sometimes, pass by reference could be at the choice of the caller, NOT the function definitition. PHP doesn’t allow it, but it
would be meaningful for the caller to decide to pass data in as a reference. i.e. «I’m done with the variable, it’s OK to stomp
on it in memory».
*/
?>

A function’s argument that is an object, will have its properties modified by the function although you don’t need to pass it by reference.

In function calls, PHP clearly distinguishes between missing arguments and present but empty arguments. Thus:

The best approach, it seems to me, is to always use a sentinel like null as the default value of an optional argument. This way, callers like g and g’s clients have many options, and furthermore, callers always know how to omit arguments so they can omit one in the middle of the parameter list.

PASSING A «VARIABLE-LENGTH ARGUMENT LIST OF REFERENCES» TO A FUNCTION
As of PHP 5, Call-time pass-by-reference has been deprecated, this represents no problem in most cases, since instead of calling a function like this:
myfunction($arg1, &$arg2, &$arg3);

provided you have defined your function as
function myfuncion($a1, &$a2, &$a3) < // so &$a2 and &$a3 are
// declared to be refs.
.
>

In the following code I tried to amend this by using the
array() language-construct as the actual argument in the
call to the function.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *