Solved

PHP OOP Connect function to another

Posted on 2014-02-22
4
459 Views
Last Modified: 2014-02-23
I'm not sure how to ask this question and I don't have a real world example or well written code to show you, but I will do my best.
I have a Wordpress plugin that has a Functions.php file.
Function myContact() within the functions.php file creates a table.  
Each of the tables has a specific ID.
Each <tr> within the table has a specific ID.
Like so:
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>

</table>";
}

Open in new window


I don't want to mess with the plugin core functions, cause if there is an update, I will loose the work. As you know.
So, I want to learn how to attach a custom function to another function. I want to attach my function to the table in the function above.  Like so:
My Custom Function.PHP file
function myCustomFunction() {
echo '<tr id='field_three'>
<td>EMAIL</td><td>CELL PHONE</>
</tr>
<tr id='field_four'>
<td>BIRTHDAY</td><td>NICK NAME</>
</tr>
}
add_action( 'myCustomFunction', myContact' ); INSERT AFTER tr id='field_two'

Open in new window


I want to insert my function after a specific <tr id> in the original function.
Does that make sense???
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39879707
I don't think that is going to work. Usually the function you are overriding should be written to support overridding - something like
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>";
 if (function_exists('customMyContact')) {
   customMyContact();
 }
echo "
</table>";
}

Open in new window

                                 
You can now extend the function by defining your customMyContact function.

However because the pluggin generates the complete table in one function with no external calls your only option is to replace the function with your own, modify the function to call your own or use javascript to modify the table at load time.

Either of the first two options fall foul of the pluggin upgrade overwrite problem.

The javascript option will work - basically you add some javascript code that appends the required information to the table. With JQuery it would be something like this

<script type="text/javascript">
// POPULATE YOUR HTML STRING HERE WITH PHP CODE INSERTS
var html="tr id='field_three'><td>EMAIL</td><td><?php echo $cell_phone;?></></tr><tr id='field_four'><td><?php echo $dob;?></td><td><?php echo $nickname;?></></tr>";
$(function() {
  $('#table_one').append(html);
});
</script>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39879721
OK, another question, what does the "10' and '2' signify at the end?
add_action('personal_details_list', 'custom_purchase_details', 10, 2);
0
 
LVL 56

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 39880631
The 10 paramter refers to the priority given to the action - the lower the number the sooner it executes. The default is 10

The last parameter refers to the number of parameters the hook accepts.

You can read more about the add_action call here
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question