Solved

PHP OOP Connect function to another

Posted on 2014-02-22
4
456 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
  • 2
4 Comments
 
LVL 54

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 54

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article, we will look at our content management system as a whole, to get an idea of how we will start to manage these content items.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
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…

832 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